Iteration tags

Iteration tags repeatedly run blocks of code.

for

Repeatedly executes a block of code. For a full list of attributes available within a for loop, see forloop (object).

for loops can output a maximum of 50 results per page. In cases where there are more than 50 results, use the paginate tag to split them across multiple pages.

else

Specifies a fallback case for a for loop which will run if the loop has zero length (for example, you loop over a collection that has no products):

break

Causes the loop to stop iterating when it encounters the break tag.

continue

Causes the loop to skip the current iteration when it encounters the continue tag.

for tag parameters

limit

Exits the for loop at a specific index.

offset

Starts the for loop at a specific index.

range

Defines a range of numbers to loop through. You can define the range using both literal and variable values.

reversed

Reverses the order of the loop.

cycle

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

cycle must be used within a for loop block.

Uses for cycle include:

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

cycle tag parameters

cycle accepts a parameter called cycle group in cases where you need multiple cycle blocks in one template. If no name is supplied for the cycle group, then it is assumed that multiple calls with the same parameters are one group.

The example below shows why cycle groups are necessary when there are multiple instances of the cycle block.

In the example above, if the first collection only has two products, the second collection loop will continue the cycle where the first one left off. This will result in this undesired output:

To avoid this, you can use a cycle group for each cycle block, as shown below:

With the code above, the two cycle blocks are independent of each other. The result is shown below:

tablerow

Generates rows for an HTML table. Must be wrapped in an opening <table> and closing </table> HTML tags. For a full list of attributes available within a tablerow loop, see tablerow (object).