Crate tera
Tera
A powerful, fast and easy-to-use template engine for Rust
This crate provides an implementation of the Tera template engine, which is designed for use in Rust applications. Inspired by Jinja2 and Django templates, Tera provides a familiar and expressive syntax for creating dynamic HTML, XML, and other text-based documents. It supports template inheritance, variable interpolation, conditionals, loops, filters, and custom functions, enabling developers to build complex applications with ease.
See the site for more information and to get started.
Features
- High-performance template rendering
- Safe and sandboxed execution environment
- Template inheritance and includes
- Expressive and familiar syntax
- Extensible with custom filters and functions
- Automatic escaping of HTML/XML by default
- Template caching and auto-reloading for efficient development
- Comprehensive error messages and debugging information
Example
use Tera;
// Create a new Tera instance and add a template from a string
let mut tera = new;
tera.add_raw_template.unwrap;
// Prepare the context with some data
let mut context = new;
context.insert;
// Render the template with the given context
let rendered = tera.render.unwrap;
assert_eq!;
Getting Started
Add the following to your Cargo.toml file:
[dependencies]
# By default no features are selected.
tera = {version = "2", features = ["fast"]}
A few features requiring more dependencies are available:
fast: speed up template rendering (you can also select only some of the features from that feature group)glob_fs: allows loading template on the filesystem using a globunicode: if you want Tera to work with graphemes clusters rather than utf-8 characters when iterating on stringspreserve_order: keep order of insertion for values
Unless you want to trim down completely on your dependencies, the fast feature should be enabled.
Then, consult the official documentation and examples to learn more about using Tera in your Rust projects.
Modules
-
value
The value type used by Tera and supporting types (
Key,Map,Number,ValueKind).
Structs
- ComponentArg Information about a single component argument.
- ComponentInfo Information about a component definition.
- Context The struct that holds the context of a template rendering.
-
Delimiters
This allows customizing the delimiters used for blocks, variables, and comments in case
you want to template files that contains text like
{{, like LaTeX. Delimiters need to be exactly 2 bytes long (e.g.{{,«). - Error The Error struct for Tera.
- Kwargs The keyword arguments of a filter/function
- ReportError An error that knows how to present itself nicely, with the right spans/notes etc.
- Span The position of a node in the template source
- State The state of the interpreter. We pass it around rather than put it on the VM to avoid multiple borrow issues when dealing with inheritance.
- Tera Main point of interaction in this library.
- Value The Value type that Tera uses internally and that will handle ser/de
Enums
- ComponentArgType The type of component arguments.
- ErrorKind All the kind of errors Tera can produce. Non-exhaustive so we can add more if needed without a breaking change.
- Number Simpler representation of numbers so operations are simpler to handle Also can be used for custom filters/tests/fn when you want to ensure you get a number
Traits
- ArgFromValue Converts a template Value into a type that can be used in Rust code
- Filter The filter function type definition
- Function The function function type definition
-
FunctionResult
The trait that automatically converts a value into a
TeraResult<Value> - Test The test function type definition
-
TestResult
Converts a custom test return value into a
TeraResult<bool>.
Functions
- escape_html Escape HTML following OWASP
Type Aliases
- EscapeFn The escape function type definition
- Map The internal HashMap type used by Tera.
- TeraResult A custom Result type for this library
Macros
- context Creates a context from key value pairs