Struct gtmpl::Template[][src]

pub struct Template {
    pub name: String,
    pub text: String,
    pub funcs: HashMap<String, Func>,
    pub tree_set: HashMap<String, Tree>,
}

The main template structure.

Fields

Methods

impl<'b> Template
[src]

impl Template
[src]

Creates a new empty template with a given name.

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!");

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!");

Parse the given text as template body.

Example

let mut tmpl = gtmpl::Template::default();
tmpl.parse("Hello World!").unwrap();

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

impl Default for Template
[src]

Returns the "default value" for a type. Read more

Auto Trait Implementations

impl Send for Template

impl Sync for Template