[英]symfony2.1: count doctrine collection in twig template
我有一个学说实体,有一个实体( 儿童 )的集合。 现在我想计算实体并打印出计数。 像这样的东西:
<div class="item">
<h1>{{ object.name }}</h1>
<div class="childrenCount">children {% count (object.children) %}</div>
</div>
我发现了一些不起作用的例子( 例如使用“计数”过滤器 导致“未找到过滤器”错误 )。
如此处所示 ,使用doctrine可以选择在处理教义集合时使用“count”方法。 否则,您可以使用“长度”过滤器。
示例代码:
<ul class="summary">
<li> {{ object.children | length }}</li>
<!-- or, use the count method of doctrine collections directly -->
<li> {{ object.children.count }}</li>
</ul>
您可以使用“长度”示例:
{% if users|length > 10 %}
...
{% endif %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.