Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Php 如何转换twig中的数组块_Php_Arrays_Twig - Fatal编程技术网

Php 如何转换twig中的数组块

Php 如何转换twig中的数组块,php,arrays,twig,Php,Arrays,Twig,我想把下面这行从PHP转换成twig,我尝试了很多方法,但是没有人能指导我怎么做 <?php foreach (array_chunk($images, 4) as $image) { ?> 及 数组\u chunk是内置的细枝作为-过滤器 通过将if移动到for循环中,可以缩短上述示例 {% for image in images|slice(0,4) if image.type == 'image' %} {# I am an image #} {% endfor

我想把下面这行从PHP转换成twig,我尝试了很多方法,但是没有人能指导我怎么做

<?php foreach (array_chunk($images, 4) as $image) { ?>


数组\u chunk
是内置的
细枝
作为-过滤器

通过将
if
移动到
for循环中,可以缩短上述示例

{% for image in images|slice(0,4) if image.type == 'image' %}
    {# I am an image #}
{% endfor %}
使用Twig的内置批处理()过滤器 将原始数组拆分为多个块。 查看此示例以获得更好的说明:

{% 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>
{%set items=['a','b','c','d','e','f','g']%}
{#batch()的第一个参数是批的大小}
{#第二个参数是要为缺少的项显示的文本#}
{项目中的行的百分比|批处理(3,'无项目')%}
{第%行中的列为%1}
{{column}}
{%endfor%}
{%endfor%}
这将被表述为:

<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>

A.
B
C
D
E
F
G
没有项目
没有项目

我对twig不熟悉,你能详细描述一下吗?哪部分?我在答案中添加了一个指向slice文档的链接
{% 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>