Iteration
Iteration tags control the flow of code, running that code multiple times against successive values in an array.

for

The for tag runs its code against every value found in an array.
1
{% for product in products %}
2
{% assign product_tags = product.tags | split: ", " %}
3
{% endfor %}
Copied!
1
{% assign seconds_per_day = 86400 %}
2
​
3
{% for i in (1..days_ahead) %}
4
{% assign time_ahead = 0 | plus: seconds_per_day | times: i %}
5
{% endfor %}
Copied!

The forloop object

The forloopobject contains information about the loop currently being processed. Learn about its contents from Shopify.
1
{% for tag in tags %}
2
{% if forloop.first %}
3
{% comment %}
4
This is the first tag in the set!
5
{% endif %}
6
{% endif %}
7
{% endfor %}
Copied!

continue

Used within a for/endfor block, the continue tag instructs Liquid to skip the rest of the code in the current pass, and begin again with the next value in the array (if any).
1
{% for fulfillment in order.fulfillments %}
2
{% if fulfillment.status == "cancelled" %}
3
{% comment %}
4
Skip this fulfillment, and process the next one (if any).
5
{% endcomment %}
6
{% continue %}
7
{% endif %}
8
​
9
{% assign tag_to_add = "not cancelled" %}
10
{% endfor %}
Copied!

break

Used within a for/endfor block, the break tag instructs Liquid to skip the rest of the code in the current pass, and to stop processing the array completely.
1
{% assign order_includes_compare_at_pricing = false %}
2
​
3
{% for line_item in order.line_items %}
4
{% if line_item.variant.compare_at_price != blank %}
5
{% assign order_includes_compare_at_pricing = true %}
6
{% comment %}
7
Only need to detect this once, so we can now break out of the loop.
8
{% endcomment %}
9
{% break %}
10
{% endif %}
11
{% endfor %}
Copied!
Last modified 7mo ago
Copy link
Edit on GitHub