Iteration tags run blocks of code repeatedly.

for

Runs a block of code repeatedly. See forloop (object) for all attributes available inside a for loop.

for loops output up to 50 results per page. If there are more than 50 results, use the paginate tag to split them across several pages.

{% for product in collection.products %}
  {{ product.title }}
{% endfor %}
Fancy hat Fancy shirt Fancy pants

else

Defines a fallback for a for loop that runs when the loop has zero length (for example when you loop over a collection without products):

{% for product in collection.products %}
  {{ product.title }}
{% else %}
  This collection is empty.
{% endfor %}
This collection is empty.

break

Stops the loop when it reaches the break tag.

{% for i in (1..5) %}
  {% if i == 4 %}
    {% break %}
  {% else %}
    {{ i }}
  {% endif %}
{% endfor %}
1 2 3

continue

Skips the current iteration when the loop reaches the continue tag.

{% for i in (1..5) %}
  {% if i == 4 %}
    {% continue %}
  {% else %}
    {{ i }}
  {% endif %}
{% endfor %}
1 2 3   5

for tag parameters

limit ends the loop at a specific index.

 <!-- numbers = [1,2,3,4,5] -->

  {% for item in numbers limit:2 %}
    {{ item }}
  {% endfor %}
1 2

offset starts the loop at a specific index.

 <!-- numbers = [1,2,3,4,5] -->
  {% for item in numbers offset:2 %}
    {{ item }}
  {% endfor %}
3 4 5

range defines a range of numbers to loop through. You can define the range with literal and variable values.

{% for i in (3..5) %}
  {{ i }}
{% endfor %}

{% assign my_limit = 4 %}
{% for i in (1..my_limit) %}
{{ i }}
{% endfor %}
3 4 5

1 2 3 4

reversed reverses the order of the loop.

 <!-- if array = [1,2,3,4,5,6] -->
{% for item in array reversed %}
  {{ item }}
{% endfor %}
6 5 4 3 2 1

cycle

Loops through a group of strings and outputs them in the order they were passed as parameters. Each time cycle is called, the next string is output.

cycle must be used inside a for loop.

{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
{% cycle 'one', 'two', 'three' %}
one
two
three
one

Uses of cycle include:

  • applying odd/even classes to table rows
  • applying a unique class to the last product thumbnail in a row

cycle tag parameters

cycle accepts a parameter called cycle group for cases where you need several cycle blocks in one template. If no cycle group name is given, calls with the same parameters are treated as one group.

This example shows why cycle groups are needed when there are several instances of the cycle block:

 <ul>

  {% for product in collections.collection-1.products %}

  <li{% cycle ' style="clear:both;" ', '', class="last" %}>
   <a href="{{ product.url | within: collection }}"> <img src="{{ product.featured_image.src | image_url: width: 240 }}" alt="{{ product.featured_image.alt }}"> </a>  {% endfor %}
  </li{%>
 </ul>

 <ul>

  {% for product in collections.collection-2.products %}

  <li{% cycle ' style="clear:both;" ', '', class="last" %}>
   <a href="{{ product.url | within: collection }}"> <img src="{{ product.featured_image.src | image_url: width: 240 }}" alt="{{ product.featured_image.alt }}"> </a>  {% endfor %}
  </li{%>
 </ul>

If the first collection has only two products, the second collection loop continues the cycle where the first one stopped. This leads to unwanted output:

 <ul>

  <li style="clear:both"></li>

 </ul>

 <ul>

  <li></li>

  <li class="last"></li>

  <li style="clear:both"></li>

  <li></li>

 </ul>

To avoid this, use a cycle group for each cycle block:

 <ul>

{% for product in collections.collection-1.products %}

  <li{% cycle 'group1': ' style="clear:both;" ', '', class="last" %}>
   <a href="{{ product.url | within: collection }}"> <img src="{{ product.featured_image.src | image_url: width: 240 }}" alt="{{ product.featured_image.alt }}"> </a>  {% endfor %}
  </li{%>
 </ul>

 <ul>

{% for product in collections.collection-2.products %}

  <li{% cycle 'group2': ' style="clear:both;" ', '', class="last" %}>
   <a href="{{ product.url | within: collection }}"> <img src="{{ product.featured_image.src | image_url: width: 240 }}" alt="{{ product.featured_image.alt }}"> </a>  {% endfor %}
  </li{%>
 </ul>

With this code the two cycle blocks are independent of each other. The result:

 <ul>

  <li style="clear:both"></li>

  <li></li>

 </ul>

 <!-- a new cycle group starts! -->

 <ul>

  <li style="clear:both"></li>

  <li></li>

  <li></li>

  <li class="last"></li>

 </ul>

tablerow

Generates rows for an HTML table. It must be wrapped in opening <table> and closing </table> HTML tags. See tablerow (object) for all attributes available inside a tablerow loop.

 <table>

  {% tablerow product in collection.products %}
    {{ product.title }}
  {% endtablerow %}

 </table>
 <table>

  <tbody>
   <tr class="row1">
    <td class="col1"> Cooles Shirt </td>
    <td class="col2"> Alien Poster </td>
    <td class="col3"> Batman Poster </td>
    <td class="col4"> Bullseye Shirt </td>
    <td class="col5"> Ein weiterer Vinyl-Klassiker </td>
    <td class="col6"> Tolle Jeans </td>
   </tr>
  </tbody>
 </table>

tablerow tag parameters

cols defines how many columns the table has.

 <table>

  {% tablerow product in collection.products cols:2 %}
    {{ product.title }}
  {% endtablerow %}

 </table>
 <table>

  <tbody>
   <tr class="row1">
    <td class="col1"> Cooles Shirt </td>
    <td class="col2"> Alien Poster </td>
   </tr>
   <tr class="row2">
    <td class="col1"> Batman Poster </td>
    <td class="col2"> Bullseye Shirt </td>
   </tr>
   <tr class="row3">
    <td class="col1"> Ein weiterer Vinyl-Klassiker </td>
    <td class="col2"> Tolle Jeans </td>
   </tr>
  </tbody>
 </table>

limit ends the tablerow loop after a specific index.

{% tablerow product in collection.products cols:2 limit:3 %}
  {{ product.title }}
{% endtablerow %}

offset starts the tablerow loop at a specific index.

{% tablerow product in collection.products cols:2 offset:3 %}
  {{ product.title }}
{% endtablerow %}

range defines a range of numbers to loop through. You can define the range with literal and variable numbers.

 <table>

  {% tablerow i in (3..5) %}
    {{ i }}
  {% endtablerow %}

 </table>

{% assign num = 4 %}

 <table>

  {% tablerow i in (1..num) %}
    {{ i }}
  {% endtablerow %}

 </table>