Created
January 8, 2013 06:29
-
-
Save jpetitcolas/4481722 to your computer and use it in GitHub Desktop.
How to differenciate odd and even rows with Twig?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<table> | |
{% for record in records %} | |
<tr class="record{% if loop.index is divisibleby(2) %}even{% else %}odd{% endif %}"> | |
<td>{{ record.id }}</td> | |
<td>{{ record.title }}</td> | |
</tr> | |
{% endfor %} | |
</table> |
or use the twig cycle function
{% for year in start_year..end_year %}
{{ cycle(['odd', 'even'], loop.index0) }}
{% endfor %}
divisibleby(2)
should be divisible by(2)
{% if loop.index is divisibleby(2) %}even{% else %}odd{% endif %}
could be shortened to
{{ loop.index0 is odd ? 'odd' : 'even' }}
this works too:
{% if loop.index is even %}
... {% else %}
... {% endif %}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://twig.sensiolabs.org/doc/tests/odd.html