Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break out of two loops in Shopify Liquid

Tags:

shopify

liquid

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 %}
like image 960
Modermo Avatar asked Mar 11 '23 04:03

Modermo


1 Answers

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 %}
like image 84
drip Avatar answered Mar 25 '23 04:03

drip