Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment declared integer variable

Tags:

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 %} 
like image 839
Codium Avatar asked Jan 24 '12 16:01

Codium


People also ask

How do you increment a variable by a number?

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.

What does ++ do to a variable?

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"

What is the increment of a variable?

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.

How do you increment a variable in C++?

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.


1 Answers

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

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

See the docs for the loop goodies.

like image 169
Maerlyn Avatar answered Oct 01 '22 12:10

Maerlyn