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 with if conditions

fn main() {
    let template = "
       {% for animal in animals %}
            {% if animal.real %}
                A real {{animal.name}}
            {% else %}
                A fake {{animal.name}}
            {% endif %}
        {% endfor %}
    ";

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

    let globals = liquid::object!({
        "animals": [
            {
                "name": "mouse",
                "real": true,
            },
            {
                "name": "snake",
                "real": true,
            },
            {
                "name": "oliphant",
                "real": false,
            },
        ],
    });
    let output = template.render(&globals).unwrap();
    println!("{}", output);
}

       
            
                A real mouse
            
        
            
                A real snake
            
        
            
                A fake oliphant
            
        
    

  • for
  • endfor
  • if
  • endif