3

我是 Twig 和 Symfony2 的新手。我想知道如何用 Twig 创建一个 3 列的表。我的数据来自数据库

到目前为止,我已经尝试了所有方法,但仍然没有任何效果。我在 Stackoverflow 上发现了这个关于制作 2 列表的内容,除了我之外,它工作得很好。我想要 3 列。

<table>
  {% for var in var1 %}
    {% if (loop.index % 2) %}<tr>{% endif %}
    <td>
      <div class="bloc">
        <a href="{{ path('xxxxxxx', {'id':var.id}) }}">
        <span>{{ var.name}}  </spann></a></div>
        <img src="{{ asset(var.image ) }}"  />    
      </div>
    </td>
    {% if (loop.index % 2) and loop.last %}
      <td>&nbsp</td>
    {% endif %}
    {% if (loop.index0 % 2) or loop.last %}</tr>{% endif %}
  {% endfor %}
</table>


ex: var1  contains names and pictures from database.
name1  name2  name3
name4  name5  name6
...

这就是我的 ATM

name1   name2
name3   name4   name5
name6   name7   name8
4

6 回答 6

6

我的解决方案适用于任意数量的列:

{% set columns = 3 %}
{% for name in names %}
    {% if loop.first or loop.index0 is divisibleby(columns) %}
        <tr>
    {% endif %}

    <td>{{ name }}</td>

    {% if loop.last and loop.index is not divisibleby(columns) %}
        {% for n in range(low=columns - (loop.index % columns), high=1, step=-1) %}
            <td>&nbsp;</td>
        {% endfor %}
    {% endif %}
    {% if loop.last or loop.index is divisibleby(columns) %}
        </tr>
    {% endif %}
{% endfor %}
于 2014-02-22T21:31:17.013 回答
5

正确的方法是使用批处理(array_chunk):

{% for batchResults in result.items|batch(result.total_results / columns) %}
    <div class="{{cycle(['left', 'left', 'right'], loop.index)}}">
        {% for item in batchResults %}
            <div class="{% if loop.last %}last{% endif %}">
                {{item}}
            </div>
        {% endfor %}
    </div>
{% endfor %}
于 2014-05-20T10:47:32.017 回答
1

Peekmo 的解决方案是正确的,但在 twig 中使用批处理过滤器可能有一种更简洁的方法。此过滤器将列表分成更小的子集。您可以控制大小并使用嵌套的 for 循环来显示行及其内容。文档中的示例是此问题的准确答案。过滤器还将巧妙地处理空单元格。即你在一个数组中有 8 个值并且你想要一个 3 列的表,最后一个单元格将是 emtpy

http://twig.sensiolabs.org/doc/filters/batch.html

于 2014-02-23T08:12:02.177 回答
0

你应该尝试这样的事情:

<table>
{% for var in var1%}
<tr>
    <td>Title1<td>
    <td>Title2<td>
    <td>Title3<td>
</tr>
<tr>
    <td>{{ var.attr1 }}<td>
    <td>{{ var.attr2 }}<td>
    <td>{{ var.attr3 }}<td>
</tr>
{%endfor%}
 </table>
于 2013-07-18T06:59:24.413 回答
0
<table>
<tr>

{% for var in var1%}
{% if loop.index0 is divisibleby(3) %}
</tr>
<tr>
{% endif %}
    <td>{{ var }}</td>

{% if loop.last %}
</tr>
{% endif %}

{%endfor%}
 </table>

我认为这可以解决您的问题,您必须每 3 次迭代打开标签,并且不要忘记在循环终止时关闭最后一个。

于 2013-07-18T07:11:37.620 回答
0

未经测试,但应该打印一张干净的桌子。

<table>
  {% for var in var1 %}
    {% if not (loop.index0 % 3) %}<tr>{% endif %}
    <td>
      <div class="bloc">
        <a href="{{ path('xxxxxxx', {'id':var.id}) }}">
        <span>{{ var.name}}  </spann></a></div>
        <img src="{{ asset(var.image ) }}"  />    
      </div>
    </td>
    {% if (loop.index % 2) and loop.last %}
      <td>&nbsp</td>
    {% endif %}
    {% if (loop.index % 3) and loop.last %}
      <td>&nbsp</td>
    {% endif %}
    {% if not (loop.index % 3) or loop.last %}</tr>{% endif %}
  {% endfor %}
</table>
于 2013-07-18T08:28:22.353 回答