Liquid HashMap
-
HashMap
-
We can pass in a HashMap and inside we can iterate over the key-value pairs as tuples.
-
So here too we use the
[]
with index 0 and 1 to access the key and the value.
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