Type Definition liquid::Tag [] [src]

type Tag = Fn(&str, &[Token], &LiquidOptions) -> Result<Box<Renderable>, Error>;

A trait for creating custom tags. This is a simple type alias for a function.

This function will be called whenever the parser encounters a tag and returns a new Renderable based on its parameters. The received parameters specify the name of the tag, the argument Tokens passed to the tag and the global LiquidOptions.

Minimal Example


struct HelloWorld;

impl Renderable for HelloWorld {
    fn render(&self, _context: &mut Context) -> Result<Option<String>, Error>{
        Ok(Some("Hello World!".to_owned()))
    }
}

let mut options : LiquidOptions = Default::default();
options.tags.insert("hello_world".to_owned(), Box::new(|_tag_name, _arguments, _options| {
     Ok(Box::new(HelloWorld))
}));

let template = liquid::parse("{{hello_world}}", options).unwrap();
let mut data = Context::new();
let output = template.render(&mut data);
assert_eq!(output.unwrap(), Some("Hello World!".to_owned()));