Skip to content

Instantly share code, notes, and snippets.

@mortendk
Last active April 29, 2016 09:29
Show Gist options
  • Save mortendk/927eab28d6643daec03a to your computer and use it in GitHub Desktop.
Save mortendk/927eab28d6643daec03a to your computer and use it in GitHub Desktop.
twig loop template
{# loop template #}
{% for item in items %}
{# assing foo, bar or baz to the var foo then print em out #}
{%
set var = [
cycle(["foo", "bar", "baz"], loop.index),
]
%}
{% if loop.first %}
{# first #} {{ var }}
{% elseif loop.last %}
{# last #} {{ var }}
{% elseif loop.index == "2" %}
{# test on number #} {{ var }}
{% elseif loop.revindex = "2"%}
{# test on numbers left #} {{ var }}
{% else %}
{# default #} {{ var }}
{% endif %}
{% endfor %}
variables for loops:
{{ loop.index }} - count 1 - n
{{ loop.index0 }} - count 0 - n
{{ loop.revindex }} - revers count n - 1
{{ loop.revindex0 }} - revers count n - 0
{{ loop.length }} - how many loops
{{ cycle(["even", "odd"], loop.index) }} - cycle through vars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment