4
votes

How do I break out of two loops in liquid. So far I have, which doesn't seem to work for me.

{% for x in a %}

  {% for y in b %}

    {% if y = 2 %}      
    {% break %}
    // When this loop breaks, the parent for loop should also break
    {% endif %}

  {% endfor %}

{% endfor %}
1

1 Answers

7
votes

You can add a flag and check once that is changed.

{% assign break_loop = false %}
{% for x in a %}

  {% for y in b %}

    {% if y = 2 %}      
    {% break %}
    {% assign break_loop = true %}
    // When this loop breaks, the parent for loop should also break
    {% endif %}

  {% endfor %}

  {% if break_loop %}
    {% break %}
  {% endif %}

{% endfor %}