是否可以连接对象列表的属性值来显示它?
像这样的东西:
{{ users|join(', ', username) }}
其中users
是对象,具有getUsername()
方法。
我想 join
不需要额外的参数,但是有没有解决方法可以实现类似的效果?我无法使用 __toString() 函数,因为它代表其他东西...
最佳答案
或者仅使用一个 forloop 即可获得相同的结果
{% for user in users %}
{{ user.username }}{% if not loop.last %}, {% endif %}
{% endfor %}
关于symfony - 连接 twig 中对象列表的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15830231/