0

我正在尝试在应用了批处理过滤器的 for 循环中包含一个模板但我不知道如何/是否可以为过滤器返回的列表中的每个对象包含

我看到人们在线从循环中进行选择,如下所示:

{% for result in results %}
        <tr>
            <td>{{ result[0] }}</td>
            <td>{{ result[1] }}</td>
            <td>{{ result[2] }}</td>
        </tr>
{% endfor %}

我只是不知道如何在列表中包含每个我的代码如下:

{% for post in posts | batch(2, '&nbsp;') %}
        <tr>
            <td style="Width: 10%; height: auto">
                {% include '_post.html' %}
            </td>
        </tr>
{% endfor %}
4

1 回答 1

0

在循环/过滤器结构中包含模板的部分是非常好的。

使用您的示例,要使用每个批次的部分模板构建帖子表,您需要:

带有循环/过滤器的模板,其中包含部分:

<table>
    <thead><tr>
        <th>Title</th>
        <th>Author</th>
        <th>Title</th>
        <th>Author</th>
    </tr></thead>
    <tbody>
    {% for row in posts | batch(2) %}
        {% include "row.html" %}
    {% endfor %}
    </tbody>
</table>

部分模板“ row.html”:

<tr>
    <td>{{ row[0].title }}</td>
    <td>{{ row[0].author }}</td>
    <td>{{ row[1].title }}</td>
    <td>{{ row[1].author }}</td>
</tr>

另一种选择是再次遍历批处理分区并使用更简单的部分模板:

模板:

<table>
    <thead><tr>
        <th>Title</th>
        <th>Author</th>
        <th>Title</th>
        <th>Author</th>
    </tr></thead>
    <tbody>
    {% for row in posts | batch(2) %}
        <tr>
        {% for col in row  %}
            {% include "col.html" %}
        {% endfor %}
        </tr>
    {% endfor %}
    </tbody>
</table>

和“col.html”:

<td>{{ col.title }}</td>
<td>{{ col.author }}</td>

如果它不起作用,请仔细检查变量名称、部分名称等。

于 2018-10-14T14:47:54.907 回答