Liquid HashMap



examples/liquid/liquid-hashmap/src/main.rs
use std::collections::HashMap;

fn main() {
    let template = "
        {% for color in colors %}
           {{ color[0] }} - {{ color[1] }}
        {% endfor %}
    ";

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

    let colors: HashMap<&str, u32> = HashMap::from([("red", 23), ("green", 17), ("blue", 42)]);
    println!("{colors:#?}");

    let globals = liquid::object!({
        "colors": colors,
    });
    let output = template.render(&globals).unwrap();
    println!("{output}");
    assert!(output.contains("blue - 42"));
    assert!(output.contains("red - 23"));
    assert!(output.contains("green - 17"));
}

{
    "blue": 42,
    "red": 23,
    "green": 17,
}

        
           green - 17
        
           blue - 42
        
           red - 23