pub struct Template {
pub name: String,
pub text: String,
pub funcs: HashMap<String, Func>,
pub tree_set: HashMap<String, Tree>,
}
Expand description
The main template structure.
Fields§
§name: String
§text: String
§funcs: HashMap<String, Func>
§tree_set: HashMap<String, Tree>
Implementations§
Source§impl Template
impl Template
Sourcepub fn with_name<T: Into<String>>(name: T) -> Template
pub fn with_name<T: Into<String>>(name: T) -> Template
Creates a new empty template with a given name
.
Sourcepub fn add_func(&mut self, name: &str, func: Func)
pub fn add_func(&mut self, name: &str, func: Func)
Adds a single custom function to the template.
§Example
use gtmpl::{Context, Func, FuncError, Value};
fn hello_world(_args: &[Value]) -> Result<Value, FuncError> {
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!");
Sourcepub fn add_funcs<T: Into<String> + Clone>(&mut self, funcs: &[(T, Func)])
pub fn add_funcs<T: Into<String> + Clone>(&mut self, funcs: &[(T, Func)])
Adds custom functions to the template.
§Example
use std::collections::HashMap;
use gtmpl::{Context, Func, FuncError, Value};
fn hello_world(_args: &[Value]) -> Result<Value, FuncError> {
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!");
Sourcepub fn parse<T: Into<String>>(&mut self, text: T) -> Result<(), ParseError>
pub fn parse<T: Into<String>>(&mut self, text: T) -> Result<(), ParseError>
Parse the given text
as template body.
§Example
let mut tmpl = gtmpl::Template::default();
tmpl.parse("Hello World!").unwrap();
Sourcepub fn add_template<N: Into<String>, T: Into<String>>(
&mut self,
name: N,
text: T,
) -> Result<(), TemplateError>
pub fn add_template<N: Into<String>, T: Into<String>>( &mut self, name: N, text: T, ) -> Result<(), TemplateError>
Add the given text
as a template with a name
.
§Example
use gtmpl::Context;
let mut tmpl = gtmpl::Template::default();
tmpl.add_template("fancy", "{{ . }}");
tmpl.parse(r#"{{ template "fancy" . }}!"#).unwrap();
let output = tmpl.render(&Context::from("Hello World"));
assert_eq!(&output.unwrap(), "Hello World!");
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Template
impl RefUnwindSafe for Template
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnwindSafe for Template
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more