Twig和Symfony2 - 测试数字是否均匀

Mil*_*loš 1 if-statement symfony twig

我想为每个偶数行创建一个不同颜色的表.为此,我创建了css,如果迭代索引是偶数,我将不得不检查我的twig模板.

{% if loop.index == "even" %}
Run Code Online (Sandbox Code Playgroud)

但是如何检查一个数字是否在树枝上?谢谢.

Tho*_*s K 8

Twig有一个内置的"偶数"测试:

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

...your code here

{% endif %}
Run Code Online (Sandbox Code Playgroud)


Yoa*_*net 5

您必须使用“模”运算符,如下所示:

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

...your code here

{% endif %}
Run Code Online (Sandbox Code Playgroud)