diff --git a/lib/template_render/library.json b/lib/template_render/library.json deleted file mode 100644 index f8946d4..0000000 --- a/lib/template_render/library.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "template_render", - "version": "1.0.0", - "description": "A mini template renderer" -} \ No newline at end of file diff --git a/lib/template_render/template_render.h b/lib/template_render/template_render.h deleted file mode 100644 index 1166efe..0000000 --- a/lib/template_render/template_render.h +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once - -#include - -typedef struct -{ - const char *key; - const String value; -} template_variable_t; - -template -inline String template_render(const String& format, T (&values)[n]) -{ - auto s = String(format); - // Conditional sections - for (size_t i = 0; i < n; i++) - { - // Include Section {{#expr}} - auto match_section_begin = "{{#" + String(values[i].key) + "}}"; - // Inverted section {{^expr}} - auto match_section_inverted_begin = "{{^" + String(values[i].key) + "}}"; - // End section {{/expr}} - auto match_section_end = "{{/" + String(values[i].key) + "}}"; - while (true) - { - bool inverted = false; - auto first = s.indexOf(match_section_begin); - if (first < 0) - { - inverted = true; - first = s.indexOf(match_section_inverted_begin); - if (first < 0) - break; - } - - auto second = s.indexOf(match_section_end, first + match_section_begin.length()); - if (second < 0) - break; - - // Arduino returns 0 and 1 for bool.toString() - if ((!inverted && (values[i].value == "1")) || (inverted && (values[i].value == "0"))) - s = s.substring(0, first) + s.substring(first + match_section_begin.length(), second) + s.substring(second + match_section_end.length()); - else - s = s.substring(0, first) + s.substring(second + match_section_end.length()); - } - } - - // Replace variables {{variable}} - for (size_t i = 0; i < n; i++) - s.replace("{{" + String(values[i].key) + "}}", values[i].value); - - return s; -} \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index a02bc30..2a87990 100644 --- a/platformio.ini +++ b/platformio.ini @@ -32,4 +32,5 @@ build_flags = lib_deps = prampec/IotWebConf @ ^3.2.1 - geeksville/Micro-RTSP @ ^0.1.6 \ No newline at end of file + geeksville/Micro-RTSP @ ^0.1.6 + rzeldent/micro-moustache @ ^1.0.0 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 47057af..f176995 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include @@ -68,7 +68,7 @@ void handle_root() // Wifi Modes const char *wifi_modes[] = {"NULL", "STA", "AP", "STA+AP"}; - const template_variable_t substitutions[] = { + const moustache_variable_t substitutions[] = { // Config Changed? {"ConfigChanged", String(config_changed)}, // Version / CPU @@ -112,7 +112,7 @@ void handle_root() {"RtspPort", String(RTSP_PORT)}}; web_server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); - auto html = template_render(file_data_index_html, substitutions); + auto html = moustache_render(file_data_index_html, substitutions); web_server.send(200, "text/html", html); } @@ -128,12 +128,12 @@ void handle_restart() return; } - const template_variable_t substitutions[] = { + const moustache_variable_t substitutions[] = { {"AppTitle", APP_TITLE}, {"AppVersion", APP_VERSION}, {"ThingName", iotWebConf.getThingName()}}; - auto html = template_render(file_data_restart_html, substitutions); + auto html = moustache_render(file_data_restart_html, substitutions); web_server.send(200, "text/html", html); log_v("Restarting... Press refresh to connect again"); sleep(100);