Liquid flow control: case/when



examples/liquid/liquid-case-when/src/main.rs
fn main() {
    let template = r#"
      {% case color %}
        {% when "blue" %}
            blue
        {% when "green" %}
            green
        {% else %}
            Unrecognized color
      {% endcase %}
    "#;

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

    // 1st
    let globals = liquid::object!({
        "color": "blue",
    });
    let output = template.render(&globals).unwrap();
    println!("{output}");
    assert_eq!(output.trim(), "blue");

    // 2nd
    let globals = liquid::object!({
        "color": "green",
    });
    let output = template.render(&globals).unwrap();
    println!("{}", output);
    assert_eq!(output.trim(), "green");

    // 3rd
    let globals = liquid::object!({
        "color": "red",
    });
    let output = template.render(&globals).unwrap();
    println!("{}", output);
    assert_eq!(output.trim(), "Unrecognized color");
}


      
            blue
        
    

      
            green
        
    

      
            Unrecognized color