cycle Template Tag
Example
Add a new color for each iteration in a for loop:
<ul>
{% for x in fruits %}
<li style='color:{% cycle 'red' 'green' 'blue' 'pink' %}'>
{{ x }}
</li>
{% endfor %}
</ul>
Run Example »
Definition and Usage
The cycle
tag returns different values for
different iterations in a loop.
The first iteration gets the first value, the second iteration gets the second value etc.
You can have as many values as you like.
If there are more iterations that values, the cycle resets and starts at value 1:
Example
The cycle restarts when it reaches the end, and continue until there are no more iterations:
<ul>
{% for x in fruits %}
<li style='color:{% cycle 'red' 'blue' %}'>
{{ x }}
</li>
{% endfor %}
</ul>
Run Example »
Syntax
{% cycle arg1 arg2 arg3 etc. %}
Parameters
Value | Description |
---|---|
arg1 arg2 arg3 etc. | The values that will be returned, one argument for each cycle. |