Is there a "break" tag to escape a loop in Liquid?

Lekensteyn picture Lekensteyn · Nov 6, 2011 · Viewed 11.2k times · Source

How do I break out of loop in Liquid, mainly a for-loop? I've tried {% break %}, but that fails with There were errors saving your file: Unknown tag 'break'.

I'm trying to achieve something like:

var variants = [];
{% for item in cart.items %}
    {% if item.product.handle == "handle-name" %}
    variants = {{item.product.variants | json}};
    {% break %} // won't work
    {% endif %}
{% endfor %}

Answer

joost picture joost · Oct 14, 2013

For future visitors. Above code does work in current Liquid (gem v2.5.1).