This is taken directly from my Django template:
{% for day in days %}
<!-- {% cycle 'day' 'day' 'day last' as cls %} -->
{% rounded "black" cls %} {# Custom tag giving me rounded borders. #}
...
{% endrounded %}
{% endfor %}
I have commented out the {% cycle %}
because I only use it to set "cls
" to "day last
" every third iteration in the loop. Is there any better way to do this without adding any code to the view? (People say that logic should stay out of templates, but having it the other way around is almost worse.)
I'm not sure I understand why you have a problem, since your current solution seems to work. I don't think you need the HTML comments, since {% cycle %}
with as
doesn't output anything, but apart from that it seems fine.
However if you want another way to do it, you could use the divisibleby
filter:
{% for day in days %}
{% if forloop.counter|divisibleby:3 %}
{% rounded "black" "day last" %}
{% else %}
{% rounded "black" "day" %}
{% endif %}
{% endfor %}
but I don't think this is any better than what you have already.