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

  • Re-export some helper fns useful to write filters/fns/tests

Macros

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

Structs

  • The struct that holds the context of a template rendering.
  • The Error type
  • Represents a JSON key/value type.
  • Represents a JSON number, whether integer or floating point.
  • Main point of interaction in this library.

Enums

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

Traits

  • The filter function type definition
  • The global function type definition
  • The tester function type definition

Functions

  • Escape HTML following OWASP
  • Interpret a serde_json::Value as an instance of type T.
  • Convert a T into serde_json::Value which is an enum that can represent any valid JSON data.

Type Aliases

  • Convenient wrapper around std::Result.