Hew packages

hew.template

Mustache-lite HTML templating for Hew — safe interpolation with automatic HTML escaping

Use version 0.1.0

hew add hew.template --version 0.1.0
Declared Hew requirement
Not specified
Licence
MIT OR Apache-2.0
Published
2026-03-12T21:11:06.790Z

Dependencies

No package dependencies.

Package README

# hew.template

`hew.template` renders small Mustache-style HTML templates. Double-brace
placeholders escape HTML metacharacters; triple-brace placeholders insert
trusted, pre-rendered HTML unchanged.

The checked [render page example](examples/render_page.hew) is below; its
test pins the rendered page, so `hew test template` fails if the output
changes:

```hew
import hew.template;

fn render_page() -> string {
    let context = "name\n<Ada & Co>\nbody\n<strong>Welcome</strong>";
    template.render("<h1>Hello, {{name}}!</h1><main>{{{body}}}</main>", context)
}

fn main() {
    println(render_page());
}

#[test]
fn escapes_the_name_and_keeps_the_trusted_body() {
    assert(render_page() == "<h1>Hello, &lt;Ada &amp; Co&gt;!</h1><main><strong>Welcome</strong></main>");
}
```

Run it from the ecosystem checkout:

```sh
hew run --pkg-path . template/examples/render_page.hew
```

The program prints:

```html
<h1>Hello, &lt;Ada &amp; Co&gt;!</h1><main><strong>Welcome</strong></main>
```

Context is a newline-delimited sequence of key/value pairs. An unpaired key
has an empty value, duplicate keys use the last value, and an unknown
placeholder renders as empty text. Only LF (`\n`) separates fields; a CR
(`\r`), including the CR in a CRLF pair, remains part of its key or value.

Marker parsing commits to the longest opening marker. `{{{` therefore starts
a raw placeholder and must be closed by `}}}`. If its exact closing marker is
missing, the malformed marker and the rest of the template render literally;
the renderer does not retry at the second `{`. A valid double-brace marker
followed by an extra `}` renders its escaped value and preserves that extra
brace. A malformed raw marker is never reinterpreted as an escaped marker one
byte later: a template that opens `{{{` and never closes it renders literally
rather than silently changing meaning.

A lone `{{` or a lone `}}` has no matching marker and renders literally.

Rendering scans the context and template once, uses average constant-time
context lookup, and appends rendered UTF-8 into a growable buffer. Expected
time is linear in the context, template, and rendered output sizes. Use raw
triple-brace placeholders only for HTML you already trust.