std::text::templateGo-style text/template rendering.
Parse a template string with 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 |
parse and render_try return TemplateError:
UnclosedDirective, UnclosedBlock, UnexpectedTag, DotOutsideRange,
or UnknownDirective. render_template panics 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() {
var ctx = template.new_ctx();
ctx.set_str("name", "Alice");
ctx.set_int("age", 30);
var items: Vec<string> = Vec.new();
items.push("red");
items.push("green");
ctx.set_list("colours", items);
let t = match template.parse("Hello {{.name}}, age {{.age}}!") {
.Ok(template) => template,
.Err(_) => panic("template parse failed"),
};
let result = template.render_try(t, ctx);
match result {
.Ok(s) => println(s),
.Err(_e) => println("template render failed"),
}
}
parseParse and validate a template string.
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() {
var ctx = template.new_ctx();
ctx.set_str("greeting", "hello");
}
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() {
var ctx = template.new_ctx();
ctx.set_str("who", "world");
let t = match template.parse("hello {{.who}}!") {
.Ok(template) => template,
.Err(_) => panic("template parse failed"),
};
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 through the var self methods
set_str, set_int, set_bool, and set_list; the binding must be var.
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).
TemplateOpaque compiled template.
TemplateErrorStructured template parse/render errors.
UnclosedDirective(string)UnclosedBlock(string)UnexpectedTag(string)DotOutsideRange(string)UnknownDirective(string)DuplicateElse(string)