tera/lib.rs
1#![doc(html_root_url = "https://docs.rs/tera")]
2//! # Tera
3//!
4//! A powerful, fast and easy-to-use template engine for Rust
5//!
6//! This crate provides an implementation of the Tera template engine, which is designed for use in
7//! Rust applications. Inspired by [Jinja2] and [Django] templates, Tera provides a familiar and
8//! expressive syntax for creating dynamic HTML, XML, and other text-based documents. It supports
9//! template inheritance, variable interpolation, conditionals, loops, filters, and custom
10//! functions, enabling developers to build complex applications with ease.
11//!
12//! See the [site](http://keats.github.io/tera/) for more information and to get started.
13//!
14//! ## Features
15//!
16//! - High-performance template rendering
17//! - Safe and sandboxed execution environment
18//! - Template inheritance and includes
19//! - Expressive and familiar syntax
20//! - Extensible with custom filters and functions
21//! - Automatic escaping of HTML/XML by default
22//! - Strict mode for enforcing variable existence
23//! - Template caching and auto-reloading for efficient development
24//! - Built-in support for JSON and other data formats
25//! - Comprehensive error messages and debugging information
26//!
27//! ## Example
28//!
29//! ```rust
30//! use tera::Tera;
31//!
32//! // Create a new Tera instance and add a template from a string
33//! let mut tera = Tera::new("templates/**/*").unwrap();
34//! tera.add_raw_template("hello", "Hello, {{ name }}!").unwrap();
35//! // Prepare the context with some data
36//! let mut context = tera::Context::new();
37//! context.insert("name", "World");
38//!
39//! // Render the template with the given context
40//! let rendered = tera.render("hello", &context).unwrap();
41//! assert_eq!(rendered, "Hello, World!");
42//! ```
43//!
44//! ## Getting Started
45//!
46//! Add the following to your Cargo.toml file:
47//!
48//! ```toml
49//! [dependencies]
50//! tera = "1.0"
51//! ```
52//!
53//! Then, consult the official documentation and examples to learn more about using Tera in your
54//! Rust projects.
55//!
56//! [Jinja2]: http://jinja.pocoo.org/
57//! [Django]: https://docs.djangoproject.com/en/3.1/topics/templates/
58
59#![deny(missing_docs)]
60
61#[macro_use]
62mod macros;
63mod builtins;
64mod context;
65mod errors;
66mod filter_utils;
67mod parser;
68mod renderer;
69mod template;
70mod tera;
71mod utils;
72
73// Library exports.
74
75pub use crate::builtins::filters::Filter;
76pub use crate::builtins::functions::Function;
77pub use crate::builtins::testers::Test;
78pub use crate::context::Context;
79pub use crate::errors::{Error, ErrorKind, Result};
80// Template, dotted_pointer and get_json_pointer are meant to be used internally only but is exported for test/bench.
81#[doc(hidden)]
82pub use crate::context::dotted_pointer;
83#[doc(hidden)]
84#[allow(deprecated)]
85pub use crate::context::get_json_pointer;
86#[doc(hidden)]
87pub use crate::template::Template;
88pub use crate::tera::Tera;
89pub use crate::utils::escape_html;
90// Re-export Value and other useful things from serde
91// so apps/tools can encode data in Tera types
92pub use serde_json::value::{from_value, to_value, Map, Number, Value};
93
94// Exposes the AST if one needs it but changing the AST is not considered
95// a breaking change so it isn't public
96#[doc(hidden)]
97pub use crate::parser::ast;
98
99/// Re-export some helper fns useful to write filters/fns/tests
100pub mod helpers {
101 /// Functions helping writing tests
102 pub mod tests {
103 pub use crate::builtins::testers::{extract_string, number_args_allowed, value_defined};
104 }
105}