Struct tera::Tera
[−]
[src]
pub struct Tera { /* fields omitted */ }
The main point of interaction in this library.
Methods
impl Tera
[src]
fn new(dir: &str) -> Result<Tera>
Create a new instance of Tera, containing all the parsed templates found in the dir
glob
The example below is what the compile_templates macros expands to.
match Tera::new("templates/**/*") { Ok(t) => t, Err(e) => { println!("Parsing error(s): {}", e); ::std::process::exit(1); } }
fn render(&self, template_name: &str, data: Context) -> Result<String>
Renders a Tera template given a Context
object.
To render a template with an empty context, simply pass a new Context
object
// Rendering a template with an empty content tera.render("hello.html", Context::new());
fn value_render<T>(&self, template_name: &str, data: &T) -> Result<String> where T: Serialize
Renders a Tera template given a Serializeable
object.
If data
is not an object, an error will be returned.
tera.render("hello.html", &user);
fn one_off(input: &str, data: Context, autoescape: bool) -> Result<String>
Renders a one off template (for example a template coming from a user input)
This creates a separate instance of Tera with no possibilities of adding custom filters
or testers, parses the template and render it immediately.
Any errors will mention the one_off
template: this is the name given to the template by
Tera
let mut context = Context::new(); context.add("greeting", &"hello"); Tera::one_off("{{ greeting }} world", context);
fn value_one_off<T>(input: &str, data: &T, autoescape: bool) -> Result<String> where T: Serialize
Renders a one off template (for example a template coming from a user input) given
a Serializeable
object.
This creates a separate instance of Tera with no possibilities of adding custom filters
or testers, parses the template and render it immediately.
Any errors will mention the one_off
template: this is the name given to the template by
Tera
Tera::value_one_off("{{ greeting }} world", &user);
fn add_template(&mut self, name: &str, content: &str) -> Result<()>
Add a single template to the Tera instance
This will error if the inheritance chain can't be built, such as adding a child template without the parent one. If you want to add several templates, use Tera::add_templates
tera.add_template("new.html", "Blabla");
fn add_templates(&mut self, templates: Vec<(&str, &str)>) -> Result<()>
Add all the templates given to the Tera instance
This will error if the inheritance chain can't be built, such as adding a child template without the parent one.
tera.add_templates(vec![ ("new.html", "blabla"), ("new2.html", "hello"), ]);
fn register_filter(&mut self,
name: &str,
filter: fn(_: Value, _: HashMap<String, Value>) -> Result<Value>)
name: &str,
filter: fn(_: Value, _: HashMap<String, Value>) -> Result<Value>)
Register a filter with Tera.
If a filter with that name already exists, it will be overwritten
tera.register_filter("upper", string::upper);
fn register_tester(&mut self,
name: &str,
tester: fn(_: Option<Value>, _: Vec<Value>) -> Result<bool>)
name: &str,
tester: fn(_: Option<Value>, _: Vec<Value>) -> Result<bool>)
Register a tester with Tera.
If a tester with that name already exists, it will be overwritten
tera.register_tester("odd", testers::odd);
fn autoescape_on(&mut self, extensions: Vec<&'static str>)
Select which suffix(es) to automatically do HTML escaping on,
[".html", ".htm", ".xml"]
by default.
Only call this function if you wish to change the defaults.
// escape only files ending with `.php.html` tera.autoescape_on(vec![".php.html"]); // disable autoescaping completely tera.autoescape_on(vec![]);