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>
[src]
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 parse(dir: &str) -> Result<Tera>
[src]
Create a new instance of Tera, containing all the parsed templates found in the dir
glob
The difference with Tera::new
is that it won't build the inheritance chains automatically
so you are free to modify the templates if you need to.
You will NOT get a working Tera instance using Tera::parse
, you will need to call
tera.build_inheritance_chains()
to make it usable
let mut tera = match Tera::parse("templates/**/*") { Ok(t) => t, Err(e) => { println!("Parsing error(s): {}", e); ::std::process::exit(1); } }; tera.build_inheritance_chains()?;
fn build_inheritance_chains(&mut self) -> Result<()>
[src]
We need to know the hierarchy of templates to be able to render multiple extends level This happens at compile-time to avoid checking it every time we want to render a template This also checks for soundness issues in the inheritance chains, such as missing template or circular extends. It also builds the block inheritance chain and detects when super() is called in a place where it can't possibly work
You generally don't need to call that yourself, unless you used Tera::parse
fn render<T: Serialize>(&self, template_name: &str, data: &T) -> Result<String>
[src]
Renders a Tera template given an object that implements Serialize
.
To render a template with an empty context, simply pass a new Context
object
If data
is serializing to an object, an error will be returned.
// Rendering a template with a normal context let mut context = Context::new(); context.add("age", 18); tera.render("hello.html", &context); // Rendering a template with a struct that impl `Serialize` tera.render("hello.html", &product); // Rendering a template with an empty context tera.render("hello.html", &Context::new());
fn one_off<T: Serialize>(
input: &str,
data: &T,
autoescape: bool
) -> Result<String>
[src]
input: &str,
data: &T,
autoescape: bool
) -> Result<String>
Renders a one off template (for example a template coming from a user input) given a Context
or an object that implements Serialize
.
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, true); // Or with a struct that impl Serialize Tera::one_off("{{ greeting }} world", &user, true);
fn add_raw_template(&mut self, name: &str, content: &str) -> Result<()>
[src]
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_raw_templates(&mut self, templates: Vec<(&str, &str)>) -> Result<()>
[src]
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_raw_templates(vec![ ("new.html", "blabla"), ("new2.html", "hello"), ]);
fn add_template_file<P: AsRef<Path>>(
&mut self,
path: P,
name: Option<&str>
) -> Result<()>
[src]
&mut self,
path: P,
name: Option<&str>
) -> Result<()>
Add a single template from a path to the Tera instance. The default name for the template is
the path given, but this can be renamed with the name
parameter
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 file, use Tera::add_template_files
// Use path as name tera.add_template_file(path, None); // Rename tera.add_template_file(path, Some("index");
fn add_template_files<P: AsRef<Path>>(
&mut self,
files: Vec<(P, Option<&str>)>
) -> Result<()>
[src]
&mut self,
files: Vec<(P, Option<&str>)>
) -> Result<()>
Add several templates from paths to the Tera instance. The default name for the template is the path given, but this can be renamed with the second parameter of the tuple
This will error if the inheritance chain can't be built, such as adding a child template without the parent one.
tera.add_template_files(vec![ (path1, None), // this template will have the value of path1 as name (path2, Some("hey")), // this template will have `hey` as name ]);
fn register_filter(&mut self, name: &str, filter: FilterFn)
[src]
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: TesterFn)
[src]
Register a tester with Tera.
If a tester with that name already exists, it will be overwritten
tera.register_tester("odd", testers::odd);
fn register_global_function(&mut self, name: &str, function: GlobalFn)
[src]
Register a global function with Tera.
If a global function with that name already exists, it will be overwritten
tera.register_global_function("range", range);
fn autoescape_on(&mut self, suffixes: Vec<&'static str>)
[src]
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![]);
fn full_reload(&mut self) -> Result<()>
[src]
Re-parse all templates found in the glob given to Tera Use this when you are watching a directory and want to reload everything, for example when a file is added.
If you are adding templates without using a glob, we can't know when a template is deleted, which would result in an error if we are trying to reload that file
fn extend(&mut self, other: &Tera) -> Result<()>
[src]
Use that method when you want to add a given Tera instance templates/filters/testers to your own. If a template/filter/tester with the same name already exists in your instance, it will not be overwritten.
// add all the templates from FRAMEWORK_TERA // except the ones that have an identical name to the ones in `my_tera` my_tera.extend(&FRAMEWORK_TERA);