Giter Site home page Giter Site logo

gertvantslot / esp-razorlike Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 427 KB

Code generator, generates code for a Web-server on the ESP8266 powered from Html pages with Razor-like syntax.

License: MIT License

C# 33.11% Arduino 25.19% C 8.36% CSS 22.40% C++ 10.94%
esp8266-arduino razor-parsing-engine esp32-arduino spiffs

esp-razorlike's Introduction

esp-razorlike

Code generator, generates code for a Web-server on the ESP8266 powered from Html pages with Razor-like syntax.

Purpose

Server a Web page from an ESP8266/NodeMcu:

  • Use HTML as template
  • Dynamic HTML generation
  • Minimal program size
  • Minimal runtime size
  • Runtime performance

Overview of usage cycle

  1. Create your main .ino sketch/project, setup Wifi and ESPAsyncWebServer.
  2. Create .cshtml file's (in the data folder) with C++ mixins
  3. Run parser. This will create two .ino files that will be compiled together with your main sketch.
  4. Upload data folder to the SPIFFS filesystem.
  5. Update

Html Syntax

It uses a simplified version of the ASP.Net MVC Razor syntax.

Html code is send to the client. To insert the result of server (ESP8266) code, use an @.

<p>The temperature is @temperature()</p>

The above code will insert the result from a call the the temperature method.

Output that is of type char * will be Html encoded, for your safety. If you need your char * to be outputed as raw Html, use @Raw( Your_code() ).

To execute C++ code on the server, use @{ // C++ code here }. To switch back to HTML code, start a new tag:

<div class="row">
@{
  for(int i = 0; i < 3; i++) {
     int led = digitalRead(i);
     <div class="col-md-4">
       <p>Status of led #<strong>@i</strong> = <strong>@led</strong></p>
     </div>
  }
}
</div>

This will create a new row, with 3 columns. In each column the result of a digitalRead will be displayed.

Generated code

The generated code will read the .cshtml file from the SPIFFS filesystem, and copy this directly to the output stream, mixed with the execution of C++ code.

The above example could be parsed into something like:

_razor_fromFile(response, &file, 3558, 3943);
for(int i = 0; i < 3; i++) {
    int led = digitalRead(i);
    _razor_fromFile(response, &file, 3990, 4044);
    _razor_fromExpression(response, i);
    _razor_fromFile(response, &file, 4070, 4085);
    _razor_fromExpression(response, led);
    _razor_fromFile(response, &file, 4087, 4101);
}

Usage

Download the .zip file and extract to a folder of choice.

dotnet RazorLikeParser.dll <sketch folder>

  • <sketch folder>
    The folder where the main .ino file is located.

All <sketch folder>/data/*.cshtml files will be parsed. Results will be placed in:

  • <sketch folder>/razor_def.ino This file will contain the fixed part of the output
  • <sketch folder>/razor_impl.ino This file will contain the parsed result.

Call razor_setup(&server) from your setup() to initialize these pages.

Do not forget to upload the data folder to the Flash/SPIFFS of the ESP8266.

Example

See folder: src/Examples

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.