简体   繁体   English

Symfony2,如何跳过树枝模板中循环中重复打印的值?

[英]Symfony2, how can I skip printing repeated values in a loop in twig templates?

I have a table in this loop: 我在此循环中有一张桌子:

{% for key, value in boxes %}
     <tr>
        <td>{{ value.boxnum }}</td>
        <td>{{ value.code }}</td>
        <td>{{ value.total }}</td>
        <td>{{ value.qty }}</td>
     </tr>
{% endfor %}

Which gives a table of: 给出了以下表格:

  Box       Code   Total   Qty  
3096362    130291   27     216  
3096362    140001   18     144  
3096362    140003    4      32  
3096362    140013   14     112  

How can I get the Box # column to only print once since the rest are just repeats? 我如何才能使Box#列仅打印一次,因为其余的只是重复一次?

This is what you can do 这就是你可以做的

{% set bnum = '' %}
{% for key, value in boxes %}
 <tr>
    {% if bnum != value.boxnum %}
      {% set bnum = value.boxnum  %}
      <td>{{ value.boxnum }}</td>
    {% else %}
      <td></td>
    {% endif %}
    <td>{{ value.code }}</td>
    <td>{{ value.total }}</td>
    <td>{{ value.qty }}</td>
 </tr>
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM