Last active
February 25, 2021 12:55
-
-
Save NikLP/cb9adc963036d1ffd8896928de2b9b7a to your computer and use it in GitHub Desktop.
Set zebra-style (odd/even) classes in twig loop in drupal 8 (bootstrap) theme
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
<div{{ attributes.addClass('container') }}> | |
{% for item in items %} | |
{# NB! notation: loop.index is 1 start, loop.index0 is zero start #} | |
<div class="row {{ loop.index0 is odd ? 'zebra-odd' : 'zebra-even' }}"> | |
<div{{ item.attributes }}>{{ item.content }}</div> | |
</div> | |
{% endfor %} | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! Or if you prefer to not add an extra div:
Personally I use the
loop.index
for this, but that's a matter of taste I guess.