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 flow control: if - else

  • Liquid has simple conditionals: if that we end with endif and the optional else.
fn main() {
    let template = "
        {% if at_home %}
           {{name}} is at home
        {% else %}
           {{name}} is NOT at home
        {% endif %}
    ";

    let template = liquid::ParserBuilder::with_stdlib()
        .build()
        .unwrap()
        .parse(template)
        .unwrap();

    // 1st
    let globals = liquid::object!({
        "name": "Foo Bar",
        "at_home": true,
    });
    let output = template.render(&globals).unwrap();
    println!("{}", output);

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

        
           Foo Bar is at home
        
    

        
           Peti Bar is NOT at home
        
    

  • if
  • else
  • endif