Increment declared integer variable

twig

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 %}

Best Solution

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

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

See the docs for the loop goodies.

Related Question