- parse
- build
- object
- render
Liquid Hello World
- Depened on the liquid crate
examples/liquid/liquid-hello-world/Cargo.toml
[package] name = "liquid-hello-world" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] liquid = "0.26"
- Start with a template that is part of the Rust source code.
- We use the parse and build methods to create the template object.
- We use unwrap here which is probably not ideal, but it simlifies the examples.
- Using the liquid::object! macro we create an object from the data we would like to pass to the template.
- Using the render method we combine the data with the template and generate (render) the resuling text.
examples/liquid/liquid-hello-world/src/main.rs
fn main() { let template = liquid::ParserBuilder::with_stdlib() .build() .unwrap() .parse("Welcome to {{name}}") .unwrap(); let globals = liquid::object!({ "name": "Liquid" }); let output = template.render(&globals).unwrap(); println!("{}", output); }
Welcome to Liquid