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 for loop passing a vector or an array

  • for

  • endfor

  • We are probably more interested in passing values from variables.

  • In this example we pass a vector of strings.

fn main() {
    let template = "
        {% for color in colors %}
           {{color}}
        {% endfor %}
    ";

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

    //let colors: [&str; 3] = ["red", "green", "blue"];
    let colors = vec!["red", "green", "blue"];

    let globals = liquid::object!({
        "colors": colors,
    });
    let output = template.render(&globals).unwrap();
    println!("{}", output);
}

        
           red
        
           green
        
           blue