for 循环开始标记中的 Twig If 语句 [英] Twig If statement within for loop opening tag

查看:35
本文介绍了for 循环开始标记中的 Twig If 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,它应该计算从对象检索的 for 循环中的项目.此代码在 symfony 5.0 项目上运行,使用 php 7.2.5 和 twig-bundle 5.0

I have this code that is supposed to count items in a for loop retrieved from an object. This code is running on a symfony 5.0 project with php 7.2.5 and twig-bundle 5.0

{% set sent_mails = 0 %}

 {% for email in emails if email.status == 1 %}
    {% set sent_mails = (sent_mails + 1) %}
 {% endfor %}

{{ sent_mails }}

它给出了以下错误:

当我使用 php 7.1.3 和 twig-bundle 4.2 在 Symfony 4.2 上运行相同的代码时,一切正常,没有错误.

When I run this same code on Symfony 4.2 using php 7.1.3 and twig-bundle 4.2, everything works without an error.

twig-bundle 代码语法是否有任何更改,我没有正确使用或缺少什么?

is there any change to the twig-bundle code syntax that I am not using correctly or what am I missing?

推荐答案

我找到了一种通过使用 Twitter 用户推荐的过滤器来实现这一目标的方法:@dbrumann

I found a way of achieving this by using filter as recommended by Twitter user: @dbrumann

{% set sent_mails = 0 %}
   {% for email in emails|filter(email => email.status == 1) %}
    {% set sent_mails = (sent_mails + 1) %}
   {% endfor %}

{{ sent_mails }}

这篇关于for 循环开始标记中的 Twig If 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆