The Shopify Liquid for loop, with the examples you actually need
A reference you can copy from, built around real theme jobs: product grids, every-third-item layouts, the last few products, and collections bigger than 50.
Key takeaways
- Shopify documents a maximum of 50 iterations in a
forloop. For longer lists, usepaginate, which also keeps Shopify from fetching more than the page needs. offset: 2skips two items, whatever the docs' "1-based" wording suggests. Their own example shows it.reversedapplies afterlimit, soreversed limit: 4gives the first four backward and not the last four.forloop.indexcounts every pass, including those you skip withcontinue, so keep your own counter when you filter inside a loop.
{%- paginate collection.products by 24 -%}
<ul class="grid">
{%- for product in collection.products -%}
<li class="grid__item{% if forloop.first %} grid__item--lead{% endif %}">
<a href="{{ product.url }}">{{ product.title | escape }}</a>
{{ product.price | money }}
</li>
{%- else -%}
<li>No products here yet.</li>
{%- endfor -%}
</ul>
{{ paginate | default_pagination }}
{%- endpaginate -%}That small snippet uses most of what matters: a loop over a list, the forloop object, an else for the empty case, and paginate around it because a collection can be longer than a loop can go. The rest of this guide takes these apart with a job for each one.
The forloop object
Inside any for, Liquid gives you a forloop variable. Here's what each field holds on the third pass of a five-item list:
| Field | Meaning | Third of five |
|---|---|---|
forloop.index | Current pass, counting from 1 | 3 |
forloop.index0 | Current pass, counting from 0 | 2 |
forloop.rindex | Passes left, including this one | 3 |
forloop.rindex0 | Passes left after this one | 2 |
forloop.first | True on the first pass | false |
forloop.last | True on the last pass | false |
forloop.length | Total passes in this loop | 5 |
forloop.parentloop | The outer loop's forloop, or nil if not nested | nil |
Comma-separated lists
{%- for tag in product.tags -%}
{{ tag | escape }}{% unless forloop.last %}, {% endunless %}
{%- endfor -%}Every third item, for a repeating layout
{%- for block in section.blocks -%}
{%- assign remainder = forloop.index | modulo: 3 -%}
<div class="card{% if remainder == 0 %} card--wide{% endif %}" {{ block.shopify_attributes }}>
{{ block.settings.title | escape }}
</div>
{%- endfor -%}A note on the {%- and -%} hyphens: they strip whitespace around the tag. Without them, a comma-separated list like the one above gets line breaks between the items, which browsers show as stray spaces before the commas.
limit, offset and reversed
| You write | On a list of 1 2 3 4 5 6 you get |
|---|---|
for n in list limit: 2 | 1 2 |
for n in list offset: 2 | 3 4 5 6 |
for n in list limit: 2 offset: 2 | 3 4 |
for n in list reversed | 6 5 4 3 2 1 |
for n in list reversed limit: 2 | 2 1 |
Two surprises in that table. First, offset: Shopify's reference calls it a "1-based index to start iterating at", which would make offset: 2 start on the second item. Its own example returns the third and fourth of four products, and so does the Liquid source. Read offset: 2 as "skip two."
Second, reversed. Liquid takes the slice first and flips it afterward, so the last row doesn't give you the last two items. To get those, work out the offset yourself:
{%- comment -%} The last four blocks, newest last {%- endcomment -%}
{%- assign skip = section.blocks.size | minus: 4 | at_least: 0 -%}
{%- for block in section.blocks offset: skip -%}
{{ block.settings.title | escape }}
{%- endfor -%}Word order matters here too. reversed has to come straight after the list, before limit or offset. Written as limit: 2 reversed, it is either a syntax error or silently ignored, depending on how strictly the code is parsed.
break and continue
continue skips to the next pass and break ends the loop. The classic job for both is "show the first four products in stock." You can't write that with limit: 4, since the limit applies before you've checked anything. And you can't use forloop.index as the count, because it counts the skipped products as well:
{%- assign shown = 0 -%}
{%- for product in collection.products -%}
{%- unless product.available -%}{%- continue -%}{%- endunless -%}
{%- assign shown = shown | plus: 1 -%}
{% render 'product-card', product: product %}
{%- if shown == 4 -%}{%- break -%}{%- endif -%}
{%- endfor -%}Variables you assign inside a loop outlive it, so shown is still readable after endfor. That's useful for an "only 2 in stock" message or for deciding whether to print a "see all" link. The reverse trap is real too: an assign inside render doesn't reach the caller, because snippets have their own variable scope.
The 50 limit, and pagination
Shopify's reference says plainly: "You can do a maximum of 50 iterations with a for loop", and tells you to use paginate for more. This is the reason a collection with 200 products shows 50 on a custom page, and the rest seem to vanish with no error at all.
{%- paginate collection.products by 48 -%}
{%- for product in collection.products -%}
{% render 'product-card', product: product %}
{%- endfor -%}
{%- if paginate.pages > 1 -%}
{{ paginate | default_pagination }}
{%- endif -%}
{%- endpaginate -%}- The
formust loop over the same list named onpaginate, inside thepaginatetags. - The paginate reference gives a page size from 1 to 250. The collection object's page still mentions a limit of 50 per page for products, so if you need more than 50 per page, test it on your theme before designing around it.
- You can paginate only up to the 25,000th item in a list. Beyond that, narrow the list down first.
- Paginated lists include
collection.products,product.variants,blog.articles,search.results,customer.orders, and theproduct_listandcollection_listsettings. paginatereads the page number from the URL, so two paginated lists on one page move together. Keep it to one per page.
Looping over numbers
A range goes in parentheses, and either end can be a variable. Star ratings are the usual job:
{%- assign rating = product.metafields.reviews.rating.value.rating | round -%}
<span aria-label="{{ rating }} out of 5">
{%- for star in (1..5) -%}
<span aria-hidden="true">{% if star <= rating %}★{% else %}☆{% endif %}</span>
{%- endfor -%}
</span>A loop can also go over the results of split, which gives you a quick list from a text setting, and over objects such as a JSON metafield, where each item is a pair: pair[0] is the key and pair[1] the value. If Liquid itself is new to you, start with what Liquid is.
Nested loops and grouping
Each loop has its own forloop, and the inner one reaches the outer through parentloop. Here is a list of variants with an ID built from both positions, a common need for labels on radio inputs:
{%- for product in collection.products limit: 4 -%}
{%- for variant in product.variants limit: 6 -%}
<input
type="radio"
name="pick-{{ forloop.parentloop.index }}"
id="pick-{{ forloop.parentloop.index }}-{{ forloop.index }}"
value="{{ variant.id }}"
>
<label for="pick-{{ forloop.parentloop.index }}-{{ forloop.index }}">{{ variant.title | escape }}</label>
{%- endfor -%}
{%- endfor -%}Keep the limits on. Nested loops multiply, and Liquid's work adds to the time a page takes to start arriving. Speeding up sections covers more of that. To group a list by a value, get the unique values first and then loop twice:
{%- assign vendors = collection.products | map: 'vendor' | uniq -%}
{%- for vendor in vendors -%}
<h3>{{ vendor | escape }}</h3>
{%- assign matching = collection.products | where: 'vendor', vendor -%}
{%- for product in matching -%}
<a href="{{ product.url }}">{{ product.title | escape }}</a>
{%- endfor -%}
{%- endfor -%}Two relatives: cycle and tablerow
cycle steps through a set of values on each pass, which is shorter than modulo for alternating styles: {% cycle 'odd', 'even' %}. tablerow writes HTML table rows and cells for you with a cols parameter. It has a place in real tabular data, though for product grids you should use CSS grid and an ordinary for, since a table carries the wrong meaning for assistive technology and doesn't reflow on phones.
When a loop misbehaves
| What you see | Likely cause |
|---|---|
| Exactly 50 items, no error | The iteration limit. Wrap it in paginate |
| Nothing, no error | The list is nil. section.settings.collection not chosen, or product read on a template that has no product |
| Pagination links change nothing | The for loops over a different variable than the one on paginate |
reversed seems to do nothing | It came after limit or offset. Put it straight after the list |
| Stray spaces between items | Whitespace around tags. Use {%- and -%} |
| A count that's always too high | forloop.index counts items you skipped with continue |
| Liquid syntax error on save | Often a missing endfor. See Liquid errors |
Questions, answered
How many times can a Liquid for loop run on Shopify?
Shopify's reference states a maximum of 50 iterations and recommends the paginate tag for anything longer. A collection with more products than that needs pagination to reach the rest.
How do I get the index in a Liquid for loop?
Use forloop.index for a count starting at 1, or forloop.index0 for one starting at 0. forloop.rindex and forloop.rindex0 count backward from the end.
How do I skip an item in a Liquid loop?
Use {% continue %} inside an if or unless. It moves to the next pass. {% break %} ends the whole loop instead.
How do I show the last item first in a Liquid loop?
Add reversed right after the list: {% for item in list reversed %}. To take only the final few, calculate offset from the list's size, because reversed runs after limit.
Why does my loop show nothing in the theme editor?
Usually the list is empty until someone picks a value, such as a collection setting with nothing chosen. Add an {% else %} to the loop to show a message or placeholder.