Layer: L7 Application ยท Build flags: none (core features only)
What this example teaches
send_template() streams an HTML (or text) template, replacing each {{name}} token with a value from a resolver callback. Like the chunked writer, it never buffers the body whole - it walks the template twice (once to size, once to write), so memory use is constant regardless of page size.
A resolver maps names to values. Your callback receives a placeholder name and returns the replacement (or nullptr for empty). Because it is called twice, it must be deterministic; values formatted into a static buffer must stay valid across both passes:
static const char *resolver(const char *name) {
static char buf[32];
if (strcmp(name, "title") == 0) return "Templating Demo";
if (strcmp(name, "uptime") == 0) { snprintf(buf, sizeof(buf), "%lu", millis()); return buf; }
return nullptr;
}
...
server.send_template(slot_id, 200, "text/html", page, resolver);
Unknown, over-long, or unterminated placeholders are emitted literally, so a malformed template degrades gracefully rather than corrupting output.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--lib="." examples/L7-Application/Templating/Templating.ino
curl http://<ip>/ # the {{...}} tokens are filled in
Annotated source
The complete sketch (Templating.ino), reproduced verbatim with added explanatory comments:
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static unsigned long hit_count = 0;
static const char *resolver(const char *name)
{
static char buf[32];
if (strcmp(name, "title") == 0)
return "Templating Demo";
if (strcmp(name, "uptime") == 0)
{
snprintf(buf, sizeof(buf), "%lu", millis());
return buf;
}
if (strcmp(name, "hits") == 0)
{
snprintf(buf, sizeof(buf), "%lu", hit_count);
return buf;
}
if (strcmp(name, "heap") == 0)
{
snprintf(buf, sizeof(buf), "%u", ESP.getFreeHeap());
return buf;
}
return nullptr;
}
void handle_root(uint8_t slot_id,
HttpReq *req)
{
(void)req;
hit_count++;
static const char page[] = "<!doctype html><html><body>"
"<h1>{{title}}</h1>"
"<p>uptime: {{uptime}} ms</p>"
"<p>hits: {{hits}}</p>"
"<p>free heap: {{heap}} bytes</p>"
"</body></html>";
}
void setup()
{
Serial.begin(115200);
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
int32_t result = server.
begin(80);
if (result < 0)
{
Serial.printf("begin() failed (error %d)\n", result);
return;
}
Serial.println("Server started on port 80");
}
void loop()
{
}
Single-port HTTP server with deterministic, zero-allocation execution.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void send_template(uint8_t slot_id, int code, const char *content_type, const char *tmpl, TemplateVar resolver)
Send a response body with {{name}} placeholders substituted.
void on(const char *path, HttpMethod method, Handler callback)
Register a route handler.
void handle()
Drive the server - call every Arduino loop() iteration.
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.
@ HTTP_GET
Safe, idempotent read.
Fully-parsed HTTP/1.1 request.