2017-01-09 71 views
1

我需要设计一个数组的第一个位置与一些风格和其他4个风格的文章。Drupal 8得到索引在树枝上的一个循环

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

,我想这样的事情:

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

+0

循环索引?图像字段的html已经由字段模板生成(我认为) – 2pha

+0

如果您指的是文章列表,那么在视图中说,那么它是包含循环的view.html.twig。 这是用于节点页面,使用默认还是全内容视图模式? –

+0

我使用传情模式 –

回答

1

要获得树枝数组的第一个值,你可以简单地使用[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 %}