Crate tera

Source
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
  • Strict mode for enforcing variable existence
  • 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 tera::Tera;

// Create a new Tera instance and add a template from a string
let mut tera = Tera::new("templates/**/*").unwrap();
tera.add_raw_template("hello", "Hello, {{ name }}!").unwrap();
// Prepare the context with some data
let mut context = tera::Context::new();
context.insert("name", "World");

// Render the template with the given context
let rendered = tera.render("hello", &context).unwrap();
assert_eq!(rendered, "Hello, World!");

§Getting Started

Add the following to your Cargo.toml file:

[dependencies]
tera = "1.0"

Then, consult the official documentation and examples to learn more about using Tera in your Rust projects.

Modules§

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.
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§

escape_html
Escape HTML following OWASP
from_value
Interpret a serde_json::Value as an instance of type T.
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.