Hieromon / PageBuilder

An arduino library to create html string in the sketch for ESP8266/ESP32 WebServer.
MIT License
193 stars 40 forks source link

Exception when using PageElement with argument instead of chararray on esp8266 #37

Closed JesseRiemens closed 2 years ago

JesseRiemens commented 2 years ago

Sorry to file another issue but I found another thing that doesn't really work for me: This will work:

String listPlants(PageArgument &args) {
    return "test";
}
PageElement root_elm("<html><body>{{Plants}}</body></html>", {
    {"Plants", listPlants}
});
PageBuilder root_page("/", {root_elm});

But this results in an exception in runtime:

String listPlants(PageArgument &args) {
    return "test";
}

static const char root_element[] PROGMEM = {
"<html><body>"
"{{Plants}}"
"</body></html>"
};

PageElement root_elm(root_element, {
    {"Plants", listPlants}
});
PageBuilder root_page("/", {root_elm});

Exception:

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)

load 0x4010f000, len 3460, room 16 
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4 
tail 4
chksum 0xc9
csum 0xc9
v00054dd0
~ld
Hieromon commented 2 years ago

@JesseRiemens Sorry for the late reply.

Here are PageElement constructors:

PageElement(const char* mold, const TokenVT& sources) : _sources(sources) { setMold(mold); }
PageElement(const __FlashStringHelper* mold, const TokenVT& sources) : _sources(sources) { setMold(mold); }

But your code is:

PageElement root_elm(root_element, {
    {"Plants", listPlants}
});

In the above declaration, the interpretation of the type of root_element is const char*. In order to pass it to __FlashStringHelper*, you need to use FPSTR macro.

PageElement root_elm(FPSTR(root_element), {
    {"Plants", listPlants}
});