Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Liquid Hello World with variable

  • Using the same template as earlier we see how we can reuse the template in 3 different ways:

  • The value of "name" is hard-coded in the call to object!

  • The value of "name" is hard-coded in a variable as str.

  • The value of "name" is a String that could come from the outside world (e.g. from a file).

Welcome to Liquid
Welcome to Liquid - 2
Welcome to Liquid - 3
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);

    // 2nd
    let name = "Liquid - 2";
    let globals = liquid::object!({
        "name": name
    });
    let output = template.render(&globals).unwrap();
    println!("{}", output);

    // 3rd
    let name = String::from("Liquid - 3");
    let globals = liquid::object!({
        "name": name
    });
    let output = template.render(&globals).unwrap();
    println!("{}", output);
}