Reputation: 975
I need to design the first position of an array with some style and the other 4 articles with another style.
Is there a way to get the index in a twig loop?
I would like something like that :
<div{{ content_attributes }}>
<div class="title">
{{ label }}
</div>
<div class="body">
{{ content.body }}
</div>
<div class="link">
{{ url }}
</div>
<div class="image">
{% if loop.index == 1 %}
<img width="100" height="100" src="{{ content.field_image }}">
{% else %}
<img width="100" height="100" src="default.png">
{% endif %}
</div>
Upvotes: 1
Views: 7770
Reputation: 15275
To get the first value of an array on twig, you can simply use the [0]
index.
//dump the value of the first position of the array
{{ dump(array[0]) }}
and to get the values of the array in a for loop starting from the second position you can use:
{% for key in array|keys %}
{% set s_key = key + 1 %}
//dump the value of the array starting from the second position...
{{ dump(array[s_key].title) }}
{% endfor %}
Upvotes: 2