Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django template forloop.counter question

i have many fields in my form i was trying to apply different css to neighbour forms fields like

<li class='thiscolor' >
   <field>
</li>

<li class='thatcolor' >
   <field>
</li>

if there a way like

{% for field in form %}
    **{% if forloop.counter%2 == 0 %}**
   <li class='thiscolor'>
    {% else%}
   <li class='thatcolor'>     
    {%endif}
     {{field}}
    </li>
{% endfor %}

for forloop.counter ?

Thanks a lot!

like image 495
icn Avatar asked Aug 02 '09 19:08

icn


1 Answers

The cycle tag is designed for this type of problem:

{% for field in form %}
    <li class="{% cycle 'thiscolor' 'thatcolor' %}">{{ field }}</li>
{% endfor %}
like image 88
Jarret Hardie Avatar answered Sep 18 '22 17:09

Jarret Hardie