std::text::templateGo-style text/template rendering.
Parse a template string with parse / try_parse, then render the compiled
Template against a Ctx value. Directives inside {{...}} are
substituted; all other text passes through verbatim.
| Directive | Meaning |
|---|---|
{{.key}} | Substitute the scalar bound to key in the context |
{{.}} | Substitute the current element (inside {{range}}) |
{{if .key}}...{{end}} | Render body only when key is non-empty/non-zero |
{{if .key}}...{{else}}...{{end}} | Conditional with else branch |
{{range .key}}...{{end}} | Iterate a list; inside the body . is the item |
try_parse and render_try return TemplateError:
UnclosedDirective, UnclosedBlock, UnexpectedTag, DotOutsideRange,
or UnknownDirective. parse and render_template panic with the same
human-readable message.
Method calls such as t.render(ctx) and t.render_try(ctx) are deferred
pending cross-module trait-method dispatch support for imported stdlib user
records. Use template.render_template(t, ctx) or
template.render_try(t, ctx) instead.
Because Hew has no reflection and recursive enum/vec nesting is not yet
supported by the v0.5 codegen-front, the context is a flat Ctx value
with two independent parallel-vector maps:
string keys to string values (integers and bools
are stored pre-rendered via ctx_set_int / ctx_set_bool).string keys to flat-stored lists of string items,
indexed by per-entry start/length metadata.WHY (flat design): Vec<Entry> where Entry carries a Vec field is
rejected by the v0.5 codegen-front ("recursive owned clone/drop thunk
synthesis not implemented"). Recursive enum variants (Value::List(Vec<Value>))
trigger the same limit. The flat parallel-vec layout works within these
constraints with no expressiveness loss for the basic template surface.
WHEN: replace with a proper recursive Value type once the codegen-front
lifts the recursive-clone restriction.
WHAT: the real model is enum Value { Str(string), Num(i64), Bool(bool), Null, List(Vec<Value>), Map(Ctx) } with Ctx as HashMap<string, Value>.
import std::text::template;
fn main() {
let ctx = template.new_ctx();
template.ctx_set_str(ctx, "name", "Alice");
template.ctx_set_int(ctx, "age", 30);
let items: Vec<string> = Vec::new();
items.push("red");
items.push("green");
template.ctx_set_list(ctx, "colours", items);
let t = template.parse("Hello {{.name}}, age {{.age}}!");
let result = template.render_try(t, ctx);
match result {
Ok(s) => println(s),
Err(e) => println(e),
}
}
try_parseParse and validate a template string.
parseParse and validate a template string, panicking if it is malformed.
render_tryRender a compiled template, returning a structured error on failure.
render_templateRender a compiled template, panicking if the template is malformed.
new_ctxCreate an empty rendering context.
import std::text::template;
fn main() {
let ctx = template.new_ctx();
template.ctx_set_str(ctx, "greeting", "hello");
}
ctx_set_strBind key to the string val in the context.
template.ctx_set_str(ctx, "name", "Alice");
ctx_set_intBind key to the integer val (stored as its decimal representation).
template.ctx_set_int(ctx, "count", 42);
ctx_set_boolBind key to the boolean val ("true" or "false").
template.ctx_set_bool(ctx, "active", true);
ctx_set_listBind key to the list of strings items.
Inside a {{range .key}}...{{end}} block, each element is bound to {{.}}.
let colours: Vec<string> = Vec::new();
colours.push("red");
colours.push("blue");
template.ctx_set_list(ctx, "colours", colours);
renderRender tmpl against ctx, returning the substituted string on success.
Returns Err(message) if the template is malformed (unclosed {{,
unmatched {{end}}, or {{.}} outside a {{range}} block).
import std::text::template;
fn main() {
let ctx = template.new_ctx();
template.ctx_set_str(ctx, "who", "world");
let t = template.parse("hello {{.who}}!");
let r = template.render_try(t, ctx);
match r {
Ok(s) => println(s), // "hello world!"
Err(_) => println("template error"),
}
}
CtxTemplate rendering context.
Construct with new_ctx(), then populate with ctx_set_str,
ctx_set_int, ctx_set_bool, and ctx_set_list.
Keys for scalar (string-valued) entries.
String-rendered values for scalar entries (parallel to skeys).
Keys for list entries.
Flat concatenation of all list items across all list entries.
Start index into litems for each list entry (parallel to lkeys).
Item count for each list entry (parallel to lkeys).
RenderPairRendered template fragment plus the next byte offset to parse.
TemplateOpaque compiled template.
TemplateErrorStructured template parse/render errors.
UnclosedDirective(string)UnclosedBlock(string)UnexpectedTag(string)DotOutsideRange(string)UnknownDirective(string)