Expand description
§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
- Built-in support for JSON and other data formats
- Comprehensive error messages and debugging information
§Example
use plf::Tera;
// Create a new Tera instance and add a template from a string
let mut tera = Tera::new();
tera.register_filter("do_nothing", do_nothing_filter);
tera.load_from_glob("examples/basic/templates/**/*")?;
// Prepare the context with some data
let mut context = plf::Context::new();
context.insert("name", "World");
// Render the template with the given context
let rendered = tera.render("hello", &context)?;
assert_eq!(rendered, "Hello, World!");§Getting Started
Add the following to your Cargo.toml file:
[dependencies]
tera = "2"Then, consult the official documentation and examples to learn more about using Tera in your Rust projects.
Re-exports§
Modules§
- value
- The value type used by Tera and supporting types (
Key,Map,Number,ValueKind).
Macros§
- context
- Creates a context from key value pairs
Structs§
- Component
Arg - Information about a single component argument.
- Component
Info - 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
- 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.
Enums§
- Component
ArgType - The type of component arguments.
- Error
Kind - 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§
- Filter
- The filter function type definition
- Function
- The function function type definition
- Test
- The test function type definition
Functions§
- escape_
html - Escape HTML following OWASP
Type Aliases§
- Escape
Fn - The escape function type definition
- Tera
Result - A custom Result type for this library