Start building

Guides

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.

The Klorr teamUpdated 10 min read

  • Liquid
  • Loops
  • Pagination
  • Reference

Key takeaways

  • Shopify documents a maximum of 50 iterations in a for loop. For longer lists, use paginate, which also keeps Shopify from fetching more than the page needs.
  • offset: 2 skips two items, whatever the docs' "1-based" wording suggests. Their own example shows it.
  • reversed applies after limit, so reversed limit: 4 gives the first four backward and not the last four.
  • forloop.index counts every pass, including those you skip with continue, so keep your own counter when you filter inside a loop.
snippets/product-grid.liquid
{%- 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:

FieldMeaningThird of five
forloop.indexCurrent pass, counting from 13
forloop.index0Current pass, counting from 02
forloop.rindexPasses left, including this one3
forloop.rindex0Passes left after this one2
forloop.firstTrue on the first passfalse
forloop.lastTrue on the last passfalse
forloop.lengthTotal passes in this loop5
forloop.parentloopThe outer loop's forloop, or nil if not nestednil

Comma-separated lists

section.liquid
{%- for tag in product.tags -%}
  {{ tag | escape }}{% unless forloop.last %}, {% endunless %}
{%- endfor -%}

Every third item, for a repeating layout

section.liquid
{%- 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 writeOn a list of 1 2 3 4 5 6 you get
for n in list limit: 21 2
for n in list offset: 23 4 5 6
for n in list limit: 2 offset: 23 4
for n in list reversed6 5 4 3 2 1
for n in list reversed limit: 22 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:

section.liquid
{%- 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:

section.liquid
{%- 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.

section.liquid
{%- 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 for must loop over the same list named on paginate, inside the paginate tags.
  • 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 the product_list and collection_list settings.
  • paginate reads 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:

section.liquid
{%- 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 %}&#9733;{% else %}&#9734;{% 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:

section.liquid
{%- 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:

section.liquid
{%- 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 seeLikely cause
Exactly 50 items, no errorThe iteration limit. Wrap it in paginate
Nothing, no errorThe list is nil. section.settings.collection not chosen, or product read on a template that has no product
Pagination links change nothingThe for loops over a different variable than the one on paginate
reversed seems to do nothingIt came after limit or offset. Put it straight after the list
Stray spaces between itemsWhitespace around tags. Use {%- and -%}
A count that's always too highforloop.index counts items you skipped with continue
Liquid syntax error on saveOften 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.

Get the whole section written for you

Describe the section you want. Klorr writes the Liquid, the CSS, the schema and every theme-editor setting — code that lives in your theme and stays yours.