2017-01-09 72 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 %}