php - 如何在twig中转换数组 block

标签 php arrays twig

我想将以下行从 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/

相关文章:

javascript - 我可以使用 javascript 动态编辑 JSON 吗?

Javascript 数组 - 将两个数组合并为一个

javascript - div 中两个变量的总和

php - 如何使用 Symfony 2 FormBuilder 在编辑 View 中禁用字段

php - 在 WordPress 数据库上运行查询并根据登录用户的自定义元返回值

PHP数组,使用 'depth'的键将数组项的深度递归地添加到数组中

javascript - 如何使用 PHP 和 JS 将 URL 中的 json 数组与另一个 json 数组组合起来

twig - Twig 中不允许转换编码

javascript - Php-Codeigniter : How to exit from ajax Manually . ?

PHP - 如果是这种情况,什么都不做