Module std::text::template

Go-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.

Supported directives

DirectiveMeaning
{{.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

Errors

parse and render_try return TemplateError: UnclosedDirective, UnclosedBlock, UnexpectedTag, DotOutsideRange, or UnknownDirective. render_template panics with the same human-readable message.

Method-style rendering

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.

Data model

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:

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>.

Examples

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"),
    }
}

Contents

Functions

Function parse

pub fn parse(src: string) -> Result<Template, TemplateError>

Parse and validate a template string.

Function render_try

pub fn render_try(t: Template, ctx: Ctx) -> Result<string, TemplateError>

Render a compiled template, returning a structured error on failure.

Function render_template

pub fn render_template(t: Template, ctx: Ctx) -> string

Render a compiled template, panicking if the template is malformed.

Function new_ctx

pub fn new_ctx() -> Ctx

Create an empty rendering context.

Examples

import std.text.template;

fn main() {
    var ctx = template.new_ctx();
    ctx.set_str("greeting", "hello");
}

Function render

pub fn render(tmpl: string, ctx: Ctx) -> Result<string, string>

Render 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).

Examples

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"),
    }
}

Types

Struct Ctx

Template 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.

Fields

skeys: Vec<string>

Keys for scalar (string-valued) entries.

svals: Vec<string>

String-rendered values for scalar entries (parallel to skeys).

lkeys: Vec<string>

Keys for list entries.

litems: Vec<string>

Flat concatenation of all list items across all list entries.

lstart: Vec<i64>

Start index into litems for each list entry (parallel to lkeys).

llen: Vec<i64>

Item count for each list entry (parallel to lkeys).

Struct Template

Opaque compiled template.

Fields

src: string

Enum TemplateError

Structured template parse/render errors.

Variants

UnclosedDirective(string)
UnclosedBlock(string)
UnexpectedTag(string)
DotOutsideRange(string)
UnknownDirective(string)
DuplicateElse(string)