Skip to content

Library for the ESP8266 Arduino to process templates and stream them with the ESP8266WebServer.

License

Notifications You must be signed in to change notification settings

winder/ESPTemplateProcessor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ESPTemplateProcessor

This library is designed to work with the ESP8266 Arduino SDK and SPIFFS.

Usage

  1. Create an HTML template. Substitutions are indicated by a keyword surrounded with % symbols, like %KEY_HERE%. Here is an example:

    <html>
      <title>%TITLE%</title>
      <body>%BODY%</body>
    </html>
  2. Upload your templates to SPIFFS. There is are instruction to do so HERE.

  3. Include the library, call SPIFFS.begin() and setup a handler.

    #include "ESPTemplateProcessor.h"
    
    ...
    
    void setup(void){
      SPIFFS.begin();
    
      ...
    
      server.on("/", handleRoot);
    }
  4. Create a callback method used to fill in substitutions. The callback is defined typedef String ProcessorCallback(const String& key);:

    String indexProcessor(const String& key) {
      Serial.println(String("KEY IS ") + key);
      if (key == "TITLE") return "Hello World!";
      else if (key == "BODY") return "It works!";
    
      return "oops";
    }
  5. Now in your handler use the processor to tie everything together:

    void handleRoot() {
      if (ESPTemplateProcessor(server).send(String("/index.html"), indexProcessor)) {
        Serial.println("SUCCESS");
      } else {
        Serial.println("FAIL");
        server.send(200, "text/plain", "page not found.");
      }
    }

Example

In the example directory is a demo showing full functionality.

About

Library for the ESP8266 Arduino to process templates and stream them with the ESP8266WebServer.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published