Skip to main content

Module tera

Module tera 

Source
Expand description

Text template engine based on Jinja2/Django.


tera is a template engine inspired by Jinja2 and the Django template language. It compiles templates to an internal representation and renders them with a Context holding the variables. Templates support inheritance, includes, for loops, conditionals, filters, and custom functions.

The main entry point is the Tera struct, which loads and caches templates. For one-off rendering without managing a template collection, Tera::one_off renders a template string directly.

§Examples

Rendering a template from a string:

use tera::{Tera, Context};

let mut tera = Tera::default();
tera.add_raw_template("greeting", "Hello, {{ name }}!").unwrap();

let mut ctx = Context::new();
ctx.insert("name", "Rust");

let output = tera.render("greeting", &ctx).unwrap();
assert_eq!(output, "Hello, Rust!");

Using loops and conditionals:

use tera::{Tera, Context};

let template = "{% for item in items %}{% if item.active %}{{ item.label }}, {% endif %}{% endfor %}";

let mut tera = Tera::default();
tera.add_raw_template("list", template).unwrap();

let mut ctx = Context::new();
ctx.insert("items", &vec![
    serde_json::json!({"label": "alpha", "active": true}),
    serde_json::json!({"label": "beta", "active": false}),
    serde_json::json!({"label": "gamma", "active": true}),
]);

let output = tera.render("list", &ctx).unwrap();
assert_eq!(output, "alpha, gamma, ");

Quick one-off rendering without creating a Tera instance:

use tera::{Tera, Context};

let mut ctx = Context::new();
ctx.insert("count", &3);

let output = Tera::one_off("There are {{ count }} lights.", &ctx, false).unwrap();
assert_eq!(output, "There are 3 lights.");

Modules§

ast
The AST of Tera
helpers
Re-export some helper fns useful to write filters/fns/tests

Macros§

try_get_value
Helper macro to get real values out of Value while retaining proper errors in filters

Structs§

Context
The struct that holds the context of a template rendering.
Error
The Error type
Map
Represents a JSON key/value type.
Number
Represents a JSON number, whether integer or floating point.
Template
This is the parsed equivalent of a template file. It also does some pre-processing to ensure it does as little as possible at runtime Not meant to be used directly.
Tera
Main point of interaction in this library.

Enums§

ErrorKind
The kind of an error (non-exhaustive)
Value
Represents any valid JSON value.

Traits§

Filter
The filter function type definition
Function
The global function type definition
Test
The tester function type definition

Functions§

dotted_pointer
Lookups a dotted path in a json value contrary to the json slash pointer it’s not allowed to begin with a dot
escape_html
Escape HTML following OWASP
from_value
Interpret a serde_json::Value as an instance of type T.
get_json_pointerDeprecated
Converts a dotted path to a json pointer one
to_value
Convert a T into serde_json::Value which is an enum that can represent any valid JSON data.

Type Aliases§

Result
Convenient wrapper around std::Result.