Drupal 8 在树枝上的循环上获取索引

Drupal 8 get index on a loop on twig

我需要将数组的第一个位置设计成某种样式,将其他 4 篇文章设计成另一种样式。

有没有办法在树枝循环中获取索引?

我想要这样的东西:

<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>

要获取 twig 上数组的第一个值,您可以简单地使用 [0] 索引。

    //dump the value of the first position of the array
    {{ dump(array[0]) }}

要从第二个位置开始在 for 循环中获取数组的值,您可以使用:

{% 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 %}