This library is designed to work with the ESP8266 Arduino SDK and SPIFFS.
-
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>
-
Upload your templates to SPIFFS. There is are instruction to do so HERE.
-
Include the library, call
SPIFFS.begin()
and setup a handler.#include "ESPTemplateProcessor.h" ... void setup(void){ SPIFFS.begin(); ... server.on("/", handleRoot); }
-
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"; }
-
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."); } }
In the example directory is a demo showing full functionality.