44

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 %}
Lekensteyn
  • 64,486
  • 22
  • 159
  • 192

1 Answers1

69

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

So, you can simply do:

{% for item in cart.items %}
    {% if item.product.handle == "handle-name" %}
    variants = {{item.product.variants | json}};
       {% break %} // This will work
    {% endif %}
{% endfor %}
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
joost
  • 6,549
  • 2
  • 31
  • 36