Skip to main content
Update link to Symfony for twig loops.
Source Link
a-am
  • 2.9k
  • 1
  • 20
  • 25

Twig has loop-variablesloop-variables which can be accessed inside for-loops. loop.first will return true on the first iteration.

{% for image in entry.images %}
    <img src="{{ image.url }}" alt="{{ image.title }}">

    {% if loop.first %}
        Do stuff..
    {% endif %}
{% endfor %}

Twig has loop-variables which can be accessed inside for-loops. loop.first will return true on the first iteration.

{% for image in entry.images %}
    <img src="{{ image.url }}" alt="{{ image.title }}">

    {% if loop.first %}
        Do stuff..
    {% endif %}
{% endfor %}

Twig has loop-variables which can be accessed inside for-loops. loop.first will return true on the first iteration.

{% for image in entry.images %}
    <img src="{{ image.url }}" alt="{{ image.title }}">

    {% if loop.first %}
        Do stuff..
    {% endif %}
{% endfor %}
improved example and added explaination + doc link
Source Link
Victor
  • 8.4k
  • 1
  • 36
  • 61

I believe youTwig has loop-variables which can use "loopbe accessed inside for-loops.first" loop.first will return true on the first iteration.

{% for image in entry.images %}
    <img src="{{ image.url }}" alt="{{ image.title }}">

    {% if loop.first %}
 {# do      Do stuff..
 #   {% endif %}
{% endifendfor %}

I believe you can use "loop.first".

{% if loop.first %}
 {# do stuff #}
{% endif %}

Twig has loop-variables which can be accessed inside for-loops. loop.first will return true on the first iteration.

{% for image in entry.images %}
    <img src="{{ image.url }}" alt="{{ image.title }}">

    {% if loop.first %}
        Do stuff..
    {% endif %}
{% endfor %}
Source Link
a-am
  • 2.9k
  • 1
  • 20
  • 25

I believe you can use "loop.first".

{% if loop.first %}
 {# do stuff #}
{% endif %}