[−][src]Struct gtmpl::Template
The main template structure.
Fields
name: String
text: String
funcs: HashMap<String, Func>
tree_set: HashMap<String, Tree>
Methods
impl<'b> Template
[src]
pub fn execute<T: Write>(
&self,
writer: &'b mut T,
data: &Context
) -> Result<(), String>
[src]
&self,
writer: &'b mut T,
data: &Context
) -> Result<(), String>
pub fn render(&self, data: &Context) -> Result<String, String>
[src]
impl Template
[src]
pub fn with_name<T: Into<String>>(name: T) -> Template
[src]
Creates a new empty template with a given name
.
pub fn add_func(&mut self, name: &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!");
pub fn add_funcs<T: Into<String> + Clone>(&mut self, funcs: &[(T, 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!");
pub fn parse<T: Into<String>>(&mut self, text: T) -> Result<(), String>
[src]
Parse the given text
as template body.
Example
let mut tmpl = gtmpl::Template::default(); tmpl.parse("Hello World!").unwrap();
pub fn add_template<N: Into<String>, T: Into<String>>(
&mut self,
name: N,
text: T
) -> Result<(), String>
[src]
&mut self,
name: N,
text: T
) -> Result<(), String>
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").unwrap()); assert_eq!(&output.unwrap(), "Hello World!");
Trait Implementations
Auto Trait Implementations
impl Send for Template
impl Sync for Template
impl Unpin for Template
impl UnwindSafe for Template
impl RefUnwindSafe for Template
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,