ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
Templating - <tt>{{name}}</tt> placeholder substitution

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; // unknown -> empty
}
...
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:

// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
#include "protocore.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
PC server;
static unsigned long hit_count = 0;
// Resolver: map a placeholder name to its replacement. Returned pointers must
// stay valid for the duration of the send_template() call; the resolver is
// invoked twice (sizing pass + write pass) so it must be deterministic.
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; // unknown -> empty
}
// GET / - render an HTML page from a template.
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>";
server.send_template(slot_id, 200, "text/html", page, resolver);
}
void setup()
{
Serial.begin(115200);
init_wifi_physical(SSID, PASSWORD);
Serial.print("Connecting to WiFi");
while (!wifi_ready())
{
delay(250);
Serial.print('.');
}
uint32_t ip = pc_net_egress_ip(); // library egress IP (network byte order), no Arduino WiFi
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
server.on("/", HttpMethod::HTTP_GET, handle_root);
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()
{
server.handle();
}
Single-port HTTP server with deterministic, zero-allocation execution.
Definition protocore.h:348
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.
Definition response.cpp:120
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.
Definition physical.cpp:41
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
Definition physical.cpp:77
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Definition physical.cpp:45
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.