Module std::text::template

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

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

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-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() {
    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),
    }
}

Contents

Functions

Function try_parse

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

Parse and validate a template string.

Function parse

pub fn parse(src: string) -> Template

Parse and validate a template string, panicking if it is malformed.

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() {
    let ctx = template.new_ctx();
    template.ctx_set_str(ctx, "greeting", "hello");
}

Function ctx_set_str

pub fn ctx_set_str(ctx: Ctx, key: string, val: string)

Bind key to the string val in the context.

Examples

template.ctx_set_str(ctx, "name", "Alice");

Function ctx_set_int

pub fn ctx_set_int(ctx: Ctx, key: string, val: i64)

Bind key to the integer val (stored as its decimal representation).

Examples

template.ctx_set_int(ctx, "count", 42);

Function ctx_set_bool

pub fn ctx_set_bool(ctx: Ctx, key: string, val: bool)

Bind key to the boolean val ("true" or "false").

Examples

template.ctx_set_bool(ctx, "active", true);

Function ctx_set_list

pub fn ctx_set_list(ctx: Ctx, key: string, items: Vec<string>)

Bind key to the list of strings items.

Inside a {{range .key}}...{{end}} block, each element is bound to {{.}}.

Examples

let colours: Vec<string> = Vec::new();
colours.push("red");
colours.push("blue");
template.ctx_set_list(ctx, "colours", colours);

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

Types

Struct Ctx

Template rendering context.

Construct with new_ctx(), then populate with ctx_set_str, ctx_set_int, ctx_set_bool, and ctx_set_list.

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 RenderPair

Rendered template fragment plus the next byte offset to parse.

Fields

text: string
next_pos: i64

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)