views:

405

answers:

2

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?

A: 

Something like this might work (untested):

{% for o in something %}
 {% for c in o %}
  {% ifchanged forloop.parent.counter %}
   <div class="{% cycle 'white' 'black' %}"></div>
  {% else %}
   <div class="{% cycle 'black' 'white' %}"></div>
  {% endifchanged %}
 {% endfor %}
{% endfor %}
Vinko Vrsalovic
A: 

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).

Nick Presta