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 %}
Using + and - Operators The most simple way to increment/decrement a variable is by using the + and - operators. This method allows you increment/decrement the variable by any value you want.
Same as in other languages: ++x (pre-increment) means "increment the variable; the value of the expression is the final value" x++ (post-increment) means "remember the original value, then increment the variable; the value of the expression is the original value"
To increment a variable means to increase it by the same amount at each change. For example, your coder may increment a scoring variable by +2 each time a basketball goal is made. Decreasing a variable in this way is known as decrementing the variable value.
In C/C++, Increment operators are used to increase the value of a variable by 1. This operator is represented by the ++ symbol. The increment operator can either increase the value of the variable by 1 before assigning it to the variable or can increase the value of the variable by 1 after assigning the variable.
There's an easier way to do what you want:
{{ cycle(["even", "odd"], loop.index) }}
See the docs for the loop
goodies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With