Increment declared integer variable

Codium picture Codium · Jan 24, 2012 · Viewed 43.3k times · Source

I try to do zebra striping:

{% set counter = 0 %}
{% for entity in entities %}
  <tr class="{{ cycle(['odd', 'even'], counter) }}">
    {% counter++ %}

but I am getting error:

Unexpected tag name "counter" (expecting closing tag for the "for" tag defined near line 11)

Could somebody give me solution?

[EDIT]

My bad solution is so easy:

{% set counter = counter + 1 %}

Answer

Maerlyn picture Maerlyn · Jan 25, 2012

There's an easier way to do what you want:

{{ cycle(["even", "odd"], loop.index) }}

See the docs for the loop goodies.