Skip to main content

Module template

Module template 

Source
Expand description

Server-side HTML templates

TemplateEngine is the integration point: implement it for any engine (minijinja, tera, handlebars, …) and render with Response::html(engine.render("page.html", &ctx)?).

Templates is a small built-in engine without dependencies. Syntax (Mustache/Handlebars subset):

TagMeaning
{{ user.name }}value, HTML-escaped; missing values render as empty
{{{ html }}} or {{& html }}value, not escaped
{{#if x}}..{{else}}..{{/if}}conditional; falsy: missing, null, false, 0, "", [], {}
{{#unless x}}..{{/unless}}inverted conditional
{{#each items}}..{{else}}..{{/each}}loop; {{this}}, {{@index}}, {{@first}}, {{@last}}, {{@key}} (objects); the else part renders for a missing or empty list
{{> partial.html}}include another template with the current context
{{! comment }}ignored

Names resolve against the innermost scope first, then outward, so loop bodies can still read top-level values.

use ullun::template::Templates;

let mut t = Templates::new();
t.add("hello.html", "Hello {{name}}!{{#each tags}} #{{this}}{{/each}}").unwrap();
let out = t
    .render("hello.html", &serde_json::json!({ "name": "<Ann>", "tags": ["a", "b"] }))
    .unwrap();
assert_eq!(out, "Hello &lt;Ann&gt;! #a #b");

Structs§

Templates
Built-in template engine, see the module docs for the syntax

Traits§

TemplateEngine
Anything that can turn a named template plus data into text