liquid-rust

Liquid templating for Rust

Usage

To include liquid in your project add the following to your Cargo.toml:

toml [dependencies] liquid = "0.6"

Now you can use the crate in your code extern crate liquid;

Example: ```rust use liquid::{Renderable, Context, Value};

let template = liquid::parse("Liquid! {{num | minus: 2}}", Default::default()).unwrap();

let mut context = Context::new(); context.set_val("num", Value::Num(4f32));

let output = template.render(&mut context); asserteq!(output.unwrap(), Some("Liquid! 2".tostring())); ```

You can find a reference on Liquid syntax here.

Plugins

Cache block ( File and Redis ) : https://github.com/FerarDuanSednan/liquid-rust-cache

Extending Liquid

Create your own filters

Creating your own filters is very easy. Filters are simply functions or closures that take an input Value and a Vec<Value> of optional arguments and return a Value to be rendered or consumed by chained filters.

```rust use liquid::{Renderable, Context, Value, FilterError};

let template = liquid::parse("{{'hello' | shout}}", Default::default()).unwrap();

let mut context = Context::new();

// create our custom shout filter context.addfilter("shout", Box::new(|input, _args| { if let &Value::Str(ref s) = input { Ok(Value::Str(s.touppercase())) } else { Err(FilterError::InvalidType("Expected a string".to_owned())) } }));

let output = template.render(&mut context); asserteq!(output.unwrap(), Some("HELLO".toowned())); ```

Create your own tags

Tags are made up of two parts, the initialization and the rendering.

Initialization happens when the parser hits a Liquid tag that has your designated name. You will have to specify a function or closure that will then return a Renderable object to do the rendering.

```rust use liquid::{LiquidOptions, Renderable, Context, Error};

// our renderable object struct Shout { text: String } impl Renderable for Shout { fn render(&self, context: &mut Context) -> Result, Error>{ Ok(Some(self.text.touppercase())) } }

let mut options : LiquidOptions = Default::default();

// initialize the tag and pass a closure that will return a new Shout renderable options.registertag("shout", Box::new(|tagname, arguments, _options| { Ok(Box::new(Shout{text: arguments[0].tostring()})) }));

// use our new tag let template = liquid::parse("{{shout 'hello'}}", options).unwrap();

let mut context = Context::new(); let output = template.render(&mut context); asserteq!(output.unwrap(), Some("HELLO".toowned())); ```

Create your own tag blocks

Blocks work very similar to Tags. The only difference is that blocks contain other markup, which is why block initialization functions take another argument, a list of Elements that are inside the specified block.

For an implementation of a Shout block, see this example.


Ignore this: rust,skeptic-template extern crate skeptic; extern crate liquid; fn main() {{ {} }}