Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 如何获取for循环中的第二个元素_Loops_For Loop_Twig - Fatal编程技术网

Loops 如何获取for循环中的第二个元素

Loops 如何获取for循环中的第二个元素,loops,for-loop,twig,Loops,For Loop,Twig,我在树枝上有一个for循环。我试图访问for循环中的不同元素,以便在我的网站上的不同位置使用它们 假设我有一个名为“图像”的模板。在该模板中,我有一个for循环,如下所示: {% for image in images %} <div> <img src="{{ image | url_image }}" width="100" height="100" /> </div> {% endfor %} 我现在面临的问题是,我只需要images.html模板

我在树枝上有一个for循环。我试图访问for循环中的不同元素,以便在我的网站上的不同位置使用它们

假设我有一个名为“图像”的模板。在该模板中,我有一个for循环,如下所示:

{% for image in images %}
<div>
  <img src="{{ image | url_image }}" width="100" height="100" />
</div>
{% endfor %}
我现在面临的问题是,我只需要images.html模板中for循环的第二个元素。你是怎么做到的

所以我想要这样的东西:

<div>
{% include 'images.html' with {'second element'} only %}
</div>

{%include'images.html'和{'second element'}仅%}

有没有人知道最好的方法是什么,或者你是如何做到的?正如你所看到的,我对小树枝很陌生;)

你几乎已经找到了答案

使用以下语法:


如果您想做一些“精心设计”的事情,例如
,如果keyThx这个方法工作得很好,那么这个语法非常有用。今天学到了新东西!
<div>
{% include 'images.html' with {'second element'} only %}
</div>
{% for key,image in images if key==elementNumber %}
<div>
  <img src="{{ image | url_image }}" width="100" height="100" />
</div>
{% endfor %}
<div>
{% include 'images.html' with {elementNumber: 1} %}
</div>
<div>
  <img src="{{ images[elementNumber] | url_image }}" width="100" height="100" />
</div>