Today, I wanted to style a little bit my project.
The basic way in classic PHP project would be to use the iterator variable of your for loop with the use of modulo.

For example:

1
<div class="<?php echo ($i % 2 == 0) 'odd' : 'even' ; ?>"></div>

In twig, when you use a loop, there are several variables that you can use like loop.index, loop.first, loop.last. I won’t copy/paste the official documentation here, you can read it here twig loop documentation.

To use the modulo function in twig, just use

1
 {{ 10 % 2 }} 

And there you go, as simple as in PHP.

Then, using twig, you can do this:

1
 {% if loop.index % 2 == 0 %} 

Or in my case I needed this :

1
 {% if loop.parent.loop.index % 2 == 0 %} 

To be able to use the index of my parent loop :)

On this blog, I found the following tips :

1
 {% if loop.index divisibleby(2) %} 

Isn’t it beautiful ? But it gets even better!

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

And there you go, thank you twig !