Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow content to be defined in an array as well as a callback function. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions examples/ContentArrayHtmlTemplate/ContentArrayHtmlTemplate.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "EspHtmlTemplateProcessor.h"

void handleRoot();

const char *ssid = "WifiSSID";
const char *password = "WifiPassword";

ESP8266WebServer server;
EspHtmlTemplateProcessor templateProcessor(&server);

void setup()
{
Serial.begin(115200);
SPIFFS.begin();

WiFi.begin(ssid, password);

Serial.println("Connecting to wifi ");

// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}

server.on("/", handleRoot);

server.begin();
Serial.println("HTTP server started");

Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}

void loop()
{
server.handleClient();
}

String indexKeyProcessor(const String &key)
{
if (key == "TITLE")
return "Hello World!";
else if (key == "VAR1")
return "It works!";

return "Key not found";
}

void handleRoot()
{
ContentItem content[5];
content[0].key = "TestArray";
content[0].value = "This comes from the content array";

templateProcessor.processAndSend("/index.html", indexKeyProcessor, content, 1);
}
13 changes: 13 additions & 0 deletions examples/ContentArrayHtmlTemplate/data/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<title>test page</title>
<body>
<h1>{{TITLE}}</h1>
<p>This is en exemple page, value of VAR1 :
{{VAR1}}</p>
<p>Escaped :
{{\This is inner content}}
</p>
<p>From Content Array:
{{TestArray}}</p>
</body>
</html>
37 changes: 32 additions & 5 deletions src/EspHtmlTemplateProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
EspHtmlTemplateProcessor::EspHtmlTemplateProcessor(WebServer* server) : mServer(server) {}
EspHtmlTemplateProcessor::~EspHtmlTemplateProcessor() {}

bool EspHtmlTemplateProcessor::processAndSend(
const String& filePath, GetKeyValueCallback getKeyValueCallback)
{
ContentItem content[0];
return this->processAndSend(filePath, getKeyValueCallback, content, 0);
}

bool EspHtmlTemplateProcessor::processAndSend(const String& filePath, GetKeyValueCallback getKeyValueCallback)
bool EspHtmlTemplateProcessor::processAndSend(const String& filePath, GetKeyValueCallback getKeyValueCallback,
ContentItem* content, int contentSize)
{
// Opening the file
FileReader reader;
Expand Down Expand Up @@ -66,7 +73,7 @@ bool EspHtmlTemplateProcessor::processAndSend(const String& filePath, GetKeyValu
buffer[index - 3] = '\0';
mServer->sendContent(buffer);
}

// Set the first char of the key into the buffer as it was not an escape char
buffer[0] = ch;
index = 1;
Expand Down Expand Up @@ -97,9 +104,29 @@ bool EspHtmlTemplateProcessor::processAndSend(const String& filePath, GetKeyValu

// Get key value
buffer[index] = '\0';
String value = getKeyValueCallback(buffer);
if(value.length() > 0)
mServer->sendContent(value);
bool foundInContentArr = false;

// First check to see if the key was passed in the content array
if (content)
{
for (int i = 0; i < contentSize; i++)
{
if (content[i].key.equals(buffer) && content[i].value.length() > 0)
{
foundInContentArr = true;
mServer->sendContent(content[i].value);
}
}
}

// Get content from callback if not passed in the content array
if (!foundInContentArr)
{
String value = getKeyValueCallback(buffer);
if (value.length() > 0)
mServer->sendContent(value);
}

index = 0;
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/EspHtmlTemplateProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
#define ESCAPE_CHAR 92 // char: '\'
#define BUFFER_SIZE 100 // Buffer size for file reading

typedef String (*const GetKeyValueCallback) (const String& key);
struct ContentItem
{
String key;
String value;
};
typedef struct ContentItem ContentItem;

typedef String (*const GetKeyValueCallback)(const String& key);

class EspHtmlTemplateProcessor
{
Expand All @@ -28,6 +35,9 @@ class EspHtmlTemplateProcessor

bool processAndSend(const String& filePath, GetKeyValueCallback getKeyValueCallback);

bool processAndSend(const String& filePath, GetKeyValueCallback getKeyValueCallback,
ContentItem* content, int contentSize);

private:
void sendError(const String& errorDescription) const;
};
Expand Down