Struct gtmpl::Template
[−]
[src]
pub struct Template<'a> { pub name: &'a str, pub text: &'a str, pub funcs: HashMap<&'a str, Func>, pub tree_ids: HashMap<usize, String>, pub tree_set: HashMap<String, Tree<'a>>, }
The main template structure.
Fields
name: &'a str
text: &'a str
funcs: HashMap<&'a str, Func>
tree_ids: HashMap<usize, String>
tree_set: HashMap<String, Tree<'a>>
Methods
impl<'a> Template<'a>
[src]
fn with_name(name: &'a str) -> Template<'a>
[src]
Creates a new empty template with a given name
.
fn add_func(&mut self, name: &'a str, func: Func)
[src]
Adds a single custom function to the template.
Example
use gtmpl::{Context, Func, Value}; fn hello_world(_args: &[Value]) -> Result<Value, String> { Ok(Value::from("Hello World!")) } let mut tmpl = gtmpl::Template::default(); tmpl.add_func("helloWorld", hello_world); tmpl.parse("{{ helloWorld }}").unwrap(); let output = tmpl.render(&Context::empty()); assert_eq!(&output.unwrap(), "Hello World!");
fn add_funcs(&mut self, funcs: &[(&'a str, Func)])
[src]
Adds custom functions to the template.
Example
use std::collections::HashMap; use gtmpl::{Context, Func, Value}; fn hello_world(_args: &[Value]) -> Result<Value, String> { Ok(Value::from("Hello World!")) } let funcs = vec![("helloWorld", hello_world as Func)]; let mut tmpl = gtmpl::Template::default(); tmpl.add_funcs(&funcs); tmpl.parse("{{ helloWorld }}").unwrap(); let output = tmpl.render(&Context::empty()); assert_eq!(&output.unwrap(), "Hello World!");
fn parse(&mut self, text: &'a str) -> Result<(), String>
[src]
Parse the given text
as template body.
Example
let mut tmpl = gtmpl::Template::default(); tmpl.parse("Hello World!").unwrap();
impl<'a, 'b> Template<'a>
[src]
fn execute<T: Write>(
&self,
writer: &'b mut T,
data: &Context
) -> Result<(), String>
[src]
&self,
writer: &'b mut T,
data: &Context
) -> Result<(), String>