Ad

Break Out Of Two Loops In Shopify Liquid

- 1 answer

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 %}
Ad

Answer

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 %}
Ad
source: stackoverflow.com
Ad