【问题标题】:symfony2.1: count doctrine collection in twig templatesymfony2.1:计算树枝模板中的学说集合
【发布时间】:2012-11-23 11:45:05
【问题描述】:

我有一个包含实体集合(children)的学说实体。 现在我想计算实体并打印出计数。 像这样的:

<div class="item">
 <h1>{{ object.name }}</h1>
 <div class="childrenCount">children {% count (object.children) %}</div>
</div>

我发现了一些不起作用的示例(like using a "count" filter 导致“未找到过滤器”错误)。

【问题讨论】:

  • object.children|length 应该可以工作,确切的错误信息是什么?
  • 消息是“未找到过滤器“长度”。'
  • 完整:使用 count 它是:过滤器“count”在第 10 行的 OurBuyBundle:Mail:bill.html.twig 中不存在
  • 没有count 过滤器,但有一个length 过滤器。你用的是哪个版本的树枝?
  • 发现“count”实际上不存在:forum.symfony-project.org/viewtopic.php?t=37282&p=123891 而“length”是一个错字——多么“尴尬”。

标签: php templates doctrine-orm twig symfony-2.1


【解决方案1】:

正如here 所发现的,在处理学说集合时,可以选择使用“计数”方法。否则,您可以使用“长度”过滤器。

示例代码:

<ul class="summary">
  <li> {{ object.children | length }}</li>
  <!-- or, use the count method of doctrine collections directly -->
  <li> {{ object.children.count }}</li>
</ul>

【讨论】:

    【解决方案2】:

    您可以使用“长度” 示例:

    {% if users|length > 10 %}
    ...
    {% endif %}
    

    查看文档:http://twig.sensiolabs.org/doc/filters/length.html

    【讨论】:

    • Soooo ...就像上面已经接受的答案一样? ;-)
    猜你喜欢
    • 1970-01-01
    • 2012-07-27
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2015-09-12
    • 2018-08-10
    相关资源
    最近更新 更多