Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig and Symfony2 - test if a number is even

I would like to create a table with different color for each even row. For that, I created the css and I will have to check in my twig template if the iteration index is even or not.

{% if loop.index == "even" %}

But how to check if a number is even in twig? Thank you.

like image 656
Milos Cuculovic Avatar asked Nov 30 '22 12:11

Milos Cuculovic


2 Answers

Twig has a built in "even" test:

{% if (loop.index is even) %}

...your code here

{% endif %}
like image 182
Thomas K Avatar answered Dec 05 '22 23:12

Thomas K


You have to use the "modulo" operator as follow :

{% if(loop.index%2 == 0) %}

...your code here

{% endif %}
like image 42
Yoann Chambonnet Avatar answered Dec 05 '22 23:12

Yoann Chambonnet