42 releases (19 breaking)

0.19.0 Jul 5, 2019
0.18.2 Feb 1, 2019
0.18.1 Jan 23, 2019
0.18.0 Dec 27, 2018
0.0.1 Feb 15, 2015

#3 in Template engine

Download history 809/week @ 2019-04-22 609/week @ 2019-04-29 625/week @ 2019-05-06 613/week @ 2019-05-13 836/week @ 2019-05-20 1136/week @ 2019-05-27 741/week @ 2019-06-03 791/week @ 2019-06-10 855/week @ 2019-06-17 982/week @ 2019-06-24 1171/week @ 2019-07-01 824/week @ 2019-07-08 723/week @ 2019-07-15 753/week @ 2019-07-22 1183/week @ 2019-07-29

3,409 downloads per month
Used in 13 crates (11 directly)

MIT license

465KB
12K SLoC

liquid-rust

Liquid templating for Rust

Travis Status Appveyor Status Crates Status Coverage Status Dependency Status

Goals:

  1. Conformant. Incompatibilities with strict shopify/liquid are bugs to be fixed.
  2. Flexible. Liquid embraces variants for different domains and we want to follow in that spirit.
  3. Performant. Do the best we can within what is conformant.

Example applications using liquid-rust:

Usage

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

[dependencies]
liquid = "0.18"

Now you can use the crate in your code:

extern crate liquid;

Example:

extern crate liquid;

let template = liquid::ParserBuilder::with_liquid()
    .build().unwrap()
    .parse("Liquid! {{num | minus: 2}}").unwrap();

let mut globals = liquid::value::Object::new();
globals.insert("num".into(), liquid::value::Value::scalar(4f64));

let output = template.render(&globals).unwrap();
assert_eq!(output, "Liquid! 2".to_string());

You can find a reference on Liquid syntax here.

Customizing Liquid

Language Variants

By default, liquid-rust has no filters, tags, or blocks. You can enable the default set or pick and choose which to add to suite your application.

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.

See filters.rs for what a filter implementation looks like. You can then register it by calling liquid::ParserBuilder::filter.

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.

See include_tag.rs for what a tag implementation looks like. You can then register it by calling liquid::ParserBuilder::tag.

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.

See comment_block.rs for what a block implementation looks like. You can then register it by calling liquid::ParserBuilder::block.

Dependencies

~7.5MB
~157K SLoC