Skip to content

Instantly share code, notes, and snippets.

@morgyface
Created March 29, 2018 11:39
Show Gist options
  • Select an option

  • Save morgyface/ae532be0e2a7a91ca35f35ad557b6a1e to your computer and use it in GitHub Desktop.

Select an option

Save morgyface/ae532be0e2a7a91ca35f35ad557b6a1e to your computer and use it in GitHub Desktop.
Twig | Using cycle to apply a pattern of classes
{% set colours = ['red', 'blue', 'green', 'yellow'] %}
{% for item in services %}
<li class="{{ cycle(colours, loop.index0) }}">
<h3>{{ item.service_title }}</h3>
</li>
{% endfor %}
@morgyface
Copy link
Author

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.index to 0, the default is 1 which means we miss the first item of the pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment