Involver Developer Network : for
This page last changed on May 29, 2012 by jed.wheeler@involver.com.
OverviewThe for loop provides a simple tool to iterate through items in an array, whether that array is custom generated or originates with one of our SML Feature Blocks. Caching ConsiderationsThis tag has no caching considerations. for loop attributes
Simple Example{% for item in array %}
{{ item }}
{% endfor%}
Simple example using a custom array with limit and offset{% assign array = [1,2,3,4,5,6] %}
{% for item in array limit:2 offset:2 %}
{{ item }}
{% endfor %}
results in: 3,4 Using the forloop Context VariableYou can use the special context variable forloop anywhere within a for loop to get information about the currently iterated item.
ExamplesShow horizontal rules under each element except for the last{% for item in array%}
{{ item }}
{% unless forloop.last %}
<hr />
{% endunless %}
{% endfor%}
Using forloop position in if logicYou can use if logic inside your for loop to check which item in the list is currently being iterated and show different content for each position. <ul> {% for variable in array.feature_block %} <li>{{variable.title}} {% if forloop.index0 == 0 %} {% editable_image name:"style 1" %} {% elsif forloop.index0 == 1 %} {% editable_image name:"style 2" %} {% elsif forloop.index0 == 2 %} {% editable_image name:"style 3" %} {% elsif forloop.index0 == 3 %} {% editable_image name:"style 4" %} {% else %} Current position is {{forloop.index0}} {% endif %}</li> {% endfor %} </ul> RangesInstead of looping over an existing collection, you can define a range of numbers to iterate through. The range can be defined by both literal and variable numbers: For example, assuming item.quantity is 4: {% for i in (1..item.quantity)%}
{{ i }}
{% endfor%}
results in: 1,2,3,4 FAQThere are currently no FAQ questions for this tag. |
![]() |
Document generated by Confluence on Feb 12, 2013 09:09 |