std::html
Text made safe for HTML (escaped), and templates rendered from JSON values. A template is HTML with tags in it, parsed once and rendered as often as needed: {{path}} the value at path, escaped (a string, a number, true or false) {{{path}}} the value as it is (HTML made elsewhere) {{for x in path}}...{{end}} the body once per element of the array at path, as x {{if path}}...{{else}}...{{end}} the first part when the value is true, a non-empty string or array, or a number other than 0 (else is optional) A path is names joined by dots (page.title); its first name is a loop's variable or a member of the data. Text can't hold a {{ of its own: give it as a value. After an error, why() says which line and tag it came from. render(src, &data, &out) checks a template against data's type while compiling (see render).
Source: std/html.volt
Types
error std::html::template_error
error template_errorwhy a template can't be parsed (SYNTAX), read from its file (READ) or rendered (MISSING: a slot or a loop names a value the data doesn't have)
| Variant | |
|---|---|
SYNTAX | |
MISSING | |
READ |
struct std::html::template
struct templatea parsed template (see the top of the file)
| Field | |
|---|---|
nodes: std::vec<node> = {} |
Methods
attach fn parse(static this: template, src: str) -> template_error!templatethe template in src
attach fn read(static this: template, path: str) -> template_error!templatethe template in file path
attach fn render(this: template&, data: std::json::value&, out: std::string&) -> template_error!voidthe template with data's values, after out's text
Functions
std::html::why
fn why() -> strwhat the last template error on this thread was: its line, its tag and what's wrong with it ("line 3: {{page.title}}: no value")
std::html::escape
fn escape(s: str, out: std::string&) -> voids with &, <, >, " and ' as entities, after out's text (fine in text and in quoted attributes)
std::html::escaped
fn escaped(s: str) -> std::strings escaped (see escape)
std::html::render
<T: type> fn render(comptime src: str, data: T&, out: std::string&) -> voidtemplate src (usually @embed("page.html")) with data's values, after out's text. src is checked against data's type T while compiling: it parses, each slot's and loop's path names a field of T or of a loop's element, a loop goes over a std::vec, a slot shows text, a number or a bool, and a path goes into an optional only inside an {{if}} on it. Anything else is a compile error that names the line and the tag. T derives json (@derive(json)), and so do the structs in it: the values come from to_json(), so rendering can't fail. std::html::render(@embed("page.html"), &p, &out);