RSS Feed
^__^

modulo in Twig

Ivo Bathke, 2012/02/14 18:05

Ever wondered how to use the modulo operator in twig, the template engine of symfony?

Actually its like in PHP:

{{ 11 % 7 }}

As described here

But i guess for most use cases modulus is used to check if a number is divisible by another number.
Therefore Twig has the very conveniant divisibleby test function.

{% if loop.index is divisibleby(3) %}


Another modulo use case is to check odd and even rows in loops. There Twig has the built-in function cycle to help you.

{{ cycle(['odd', 'even'], i) }}

Once you know its so eeasy;)

One Response to “modulo in Twig”

  1. - We are php says:

    [...] modulo in Twig [...]

Leave a Reply