...

Open source softwares - Django

Back to Course

Lesson Description


Lession - #1046 Django-cycle


Cycles

The cycle tag permits you to do various undertakings for various iterations.

The cycle tag takes arguments, the main iteration utilizes the principal contention, the subsequent iteration utilizes the second contention and so forth.

{% cycle 'lightblue' 'pink' 'yellow' 'coral' 'grey' %}
To have another foundation tone for every emphasis, you can do that with the cycle tag:

Example:
<ul>
  {% for x in members %}
    <li style='background-color:{% cycle 'lightblue' 'pink' 'yellow' 'coral' 'grey' %}'>
      {{ x.firstname }}
    </li>
  {% endfor %}
</ul> 
In the event that the cycle arrives at the finish of the contentions, it begins once again:
Example
<ul>
  {% for x in members %}
    <li style='background-color:{% cycle 'lightblue' 'pink' %}'>
      {{ x.firstname }}
    </li>
  {% endfor %}
</ul> 


Cycle Arguments as Variable

In the main model the contention values was shown directly in the cycle, however you can likewise keep the contention values in a variable, and use it later:
Example:
<ul>
  {% for x in members %}
    {% cycle 'lightblue' 'pink' 'yellow' 'coral' 'grey' as bgcolor silent %}
    <li style='background-color:{{ bgcolor }}'>
      {{ x.firstname }}
    </li>
  {% endfor %}
</ul> 

Did you see the quiet keyword? Ensure you add this, or, in all likelihood the argument values will be shown two times in the result:
Example:
<ul>
  {% for x in members %}
    {% cycle 'lightblue' 'pink' 'yellow' 'coral' 'grey' as bgcolor %}
    <li style='background-color:{{ bgcolor }}'>
      {{ x.firstname }}
    </li>
  {% endfor %}
</ul> 

Reset Cycle

You can drive the cycle to restart by utilizing the {% resetcycle %} tag:
Example
Restart the cycle after 3 cycles:
<ul>
  {% for x in members %}
    {% cycle 'lightblue' 'pink' 'yellow' 'coral' 'grey' as bgcolor silent %}
    {% if forloop.counter == 3 %}
      {% resetcycle %}
    {% endif %}
    <li style='background-color:{{ bgcolor }}'>
      {{ x.firstname }}
    </li>
  {% endfor %}
</ul>