我想将以下行从 PHP 转换为 twig 我尝试了很多方法,但没有用,任何人都可以指导我如何做...
<?php foreach (array_chunk($images, 4) as $image) { ?>
和
<?php if ($image['type'] == 'image') { ?>
最佳答案
使用Twig内置的batch()过滤器
batch filter 将原始数组分割成多个 block 。 查看此示例以获得更好的说明:
{% set items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] %}
<table>
{#The first param to batch() is the size of the batch#}
{#The 2nd param is the text to display for missing items#}
{% for row in items|batch(3, 'No item') %}
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
这将呈现为:
<table>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
<tr>
<td>g</td>
<td>No item</td>
<td>No item</td>
</tr>
</table>
<强> Reference
关于php - 如何在twig中转换数组 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44795077/