0

I'm cycling through a list using {% for item in list %} and I want to output something like this:

<li>One</li>
<li class='alternate'>Two</li>
<li>Three</li>
<li class='alternate'>Four</li>
...

with the intention of styling the alternate lines a different colour.

Django (1.3) has no mod or div template tag operators. I know I can add them by manipulating the django source, but I'd rather not do that as I want to be able to package this app up.

How can I achieve this?

fredley
  • 32,953
  • 42
  • 145
  • 236

2 Answers2

1

How about django's divisibleby filter...

{% for item in list %}

<li class= "{% if forloop.counter|divisibleby:2 %}alternate{% endif %}"><Your value></li>

{% endfor %}
Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62
1

Alternatively, cycle:

<li {% cycle "class='alternate'" "" %}"
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895