Skip to content

template

HTML templates with automatic escaping: {{name}} fields, {{#if}} sections and {{#each}} loops, filled from a Map<Str, template.Value>.

Compile a template once with template.compile, then call render with the values for each page. Every Text value is HTML-escaped (& < > " '), so data cannot inject markup. Only a value wrapped in Html goes in unescaped. Both compile and render return ! TemplateError.

Fill a page with a title and a list:

import template
let page = try template.compile(&"<h1>{{title}}</h1>\n{{#if items}}<ul>{{#each items}}<li>{{name}}</li>{{/each}}</ul>{{else}}<p>Empty</p>{{/if}}")
let items = [Map { "name": template.Value.Text("tea & cake") }, Map { "name": template.Value.Text("<b>") }]
let values: Map<Str, template.Value> = Map { "title": .Text("Menu"), "items": .List(items) }
print(try page.render(&values))
<h1>Menu</h1>
<ul><li>tea &amp; cake</li><li>&lt;b&gt;</li></ul>

Put text from a request or another outside source into a page. Untrusted<Str> cannot be used as Text directly; template.untrusted turns it into an escaped value:

import template
let query = Untrusted.new("<script>alert(1)</script>")
let t = try template.compile(&"You searched for {{q}}")
print(try t.render(&Map { "q": template.untrusted(&query) }))
You searched for &lt;script&gt;alert(1)&lt;/script&gt;

Report mistakes in a template:

import template
let t = template.compile(&"{{#if admin}}<b>admin</b>") catch {
template.TemplateError(e) => {
print(e.message)
return
}
}
let _ = t
`{{#if admin}}` needs `{{/if}}`
import template
pub fn untrusted(u: &Untrusted<Str>) -> Value

Escaped text from outside: it can go into a template only through escaping.

pub fn compile(text: &Str) -> Template ! TemplateError
pub enum Value {
Text(Str)
Html(Str)
Flag(bool)
List(Vec<Map<Str, Value>>)
}

A value for a template; Text (and Untrusted) is always escaped, only Html goes in as written.

Text is escaped. Html is inserted as written: use it only for markup your program built, never for outside data. Flag is for #if and prints as true or false. List holds one Map per item for #each.

Escaping makes text safe in element content and in quoted attributes (<a title="{{t}}">). It does not make a URL safe: <a href="{{url}}"> with a javascript: URL is still a link to script. Check URLs yourself before rendering them.

pub struct TemplateError {
pub message: Str
}
pub struct Template {
}

A parsed template: {{name}}, {{#if name}}..{{else}}..{{/if}}, {{#each name}}..{{/each}}.

The syntax is small:

  • {{name}} inserts a value. Spaces just inside the braces are allowed ({{ name }}), but a name cannot contain spaces. Dots are not paths: {{user.name}} looks up a key named user.name.
  • {{#if name}}..{{/if}} and {{#if name}}..{{else}}..{{/if}}. A value is true when it is a non-empty Text or Html, a Flag(true), or a non-empty List. A missing name is false, not an error.
  • {{#each name}}..{{/each}} repeats its body once per item of a List. Inside, names are looked up in the item first, then outward, so the outer title is still visible.

There are no comments, partials, filters or {{else}} for each. Whitespace and line breaks around tags are kept as written.

It also has 1 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.

pub fn render(&self, values: &Map<Str, Value>) -> Str ! TemplateError

Fills the template; every Text value is HTML-escaped, a missing name is an error.

A {{name}} with no value is an error (“no value for name”) rather than empty output, so a typo in a template or a missing key shows up at once. A List used as {{name}} is an error too. To render optional values, set them to .Text("") or wrap the field in {{#if name}}.