Rom*_*tit 5 php twig twig-extension
看来争论没有正确通过......
{% set items = {
item: {
'id': '1',
'brand': 'client1',
'description': 'solutions.client1.description' | trans},
item: {
'id': '2',
'brand': 'client2',
'description': 'solutions.client2.description' | trans}
} %}
{% include 'site/case-excerpt.html.twig'
with {'id': items.item.id,
'brand': items.item.brand,
'description': items.item.description
}
%}
Run Code Online (Sandbox Code Playgroud)
然后在site/case-excerpt.html.twig文件中:
{% include 'site/testimonials/item.html.twig'
with {'id': id,
'brand': brand,
'description': description
}
%}
Run Code Online (Sandbox Code Playgroud)
并在site/testimonials/item.html.twig文件中:
<div class="carousel-item {{ id }}">
<img src="images/brands/{{ brand }}.jpg">
<p>
{{ description }}
</p>
</div>
Run Code Online (Sandbox Code Playgroud)
预期输出如下:
<div class="carousel-item 1">
<img src="images/brands/client1.jpg">
<p>
I'm the content of the translation for the first client
</p>
</div>
<div class="carousel-item 2">
<img src="images/brands/client2.jpg">
<p>
I'm the content of the translation for the second client
</p>
</div>
Run Code Online (Sandbox Code Playgroud)
我放弃了手动循环的想法items,因为似乎可以很好地完成它,例如这里。
这是最终的工作代码
基础文件。数组被定义并传递给包含。
{% set items = [{
'id': '1',
'brand': 'euromaster',
'description': 'solutions.euromaster.description' | trans},
{
'id': '2',
'brand': 'logo-havas-voyages',
'description': 'solutions.havas.description' | trans}
] %}
{% include 'site/case-excerpt.html.twig'
with {'items': items
}
%}
Run Code Online (Sandbox Code Playgroud)
然后在site/case-excerpt.html.twig文件中:我们循环数组并包含部分
{% for item in items %}
{% include 'site/testimonials/item.html.twig'
with {'id': item.id,
'brand': item.brand,
'description': item.description
}
%}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
并在site/testimonials/item.html.twig文件中:
<div class="carousel-item {{ id }}">
<img src="images/brands/{{ brand }}.jpg">
<p>
{{ description }}
</p>
</div>
Run Code Online (Sandbox Code Playgroud)
谢谢@Mazzy!