Change theme:   

for

"For" iterates over an array or collection. Several useful variables are available to you within the loop.

Basic usage:

   {% for item in collection %}
     {{ forloop.index }}: {{ item.name }}
   {% endfor %}

Advanced usage:

   {% for item in collection %}
     <div {% if forloop.first %}class="first"{% endif %}>
       Item {{ forloop.index }}: {{ item.name }}
     </div>
   {% endfor %}

Elements in collection can be reversed by using a reversed keyword:

   {% for item in collection reversed %}
     {{ item.name }}
   {% endfor %}

You can also define a limit and offset. Remember that offset starts at 0 for the first item.

   {% for item in collection limit:5 offset:10 %}
     {{ item.name }}
   {% endfor %}

Available variables:

forloop.name‘item-collection‘
forloop.lengthLength of the loop
forloop.indexThe current item‘s position in the collection; forloop.index starts at 1. This is helpful for non-programmers who start believe the first item in an array is 1, not 0.
forloop.index0The current item‘s position in the collection where the first item is 0
forloop.rindexNumber of items remaining in the loop (length - index) where 1 is the last item.
forloop.rindex0Number of items remaining in the loop where 0 is the last item.
forloop.firstReturns true if the item is the first item.
forloop.lastReturns true if the item is the last item.