Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of cycle in django

I have a webpage where I am looping,and using cycle inside the loop.

{% for o in something %}
{% for c in o %}
 <div class="{% cycle 'white' 'black'%}"></div>
{% endfor %}

Now, this means everytime inside the loop, first div tag gets white.But,what I want is to alternate between white and black i.e. start with white, then next time when inside the loop start the first div tag with black.Is it possible to achieve here?

like image 507
kost Avatar asked Oct 14 '22 15:10

kost


1 Answers

There is an accept bug open about this issue. You may want to try the proposed change to see if it works for you.

If you do not want to try it, or it does not work, give this a shot:

{% cycle 'white' 'black' as divcolors %}
{% for o in something %}
    {% for c in o %}
        <div class="{% cycle divcolors %}"></div>
    {% endfor %}
{% endfor %}

As I understand it, the cycle would start at white, and then loop through the values each time inside the loop (meaning you won't restart at white every time).

like image 96
Nick Presta Avatar answered Oct 19 '22 08:10

Nick Presta