Created
March 29, 2018 11:39
-
-
Save morgyface/ae532be0e2a7a91ca35f35ad557b6a1e to your computer and use it in GitHub Desktop.
Twig | Using cycle to apply a pattern of classes
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
| {% set colours = ['red', 'blue', 'green', 'yellow'] %} | |
| {% for item in services %} | |
| <li class="{{ cycle(colours, loop.index0) }}"> | |
| <h3>{{ item.service_title }}</h3> | |
| </li> | |
| {% endfor %} |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using the Twig cycle function to apply a pattern of classes
Twig's cycle works well for odd and even classes but can also be used for longer patterns like the example above.
Notice we set
loop.indexto0, the default is1which means we miss the first item of the pattern.