Twig If语句在for循环开始标记中

问题描述 投票:0回答:1

我有此代码,该代码应该用于对从对象检索的for循环中的项目进行计数。此代码在使用PHP 7.2.5和Twig 5.0的symfony 5.0项目上运行]

{% set sent_mails = 0 %}

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

{{ sent_mails }}

并且出现以下错误:

Symfony error message

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

enter image description here

我没有正确使用的树枝束代码语法有什么变化,或者我缺少什么?

php symfony twig symfony4 symfony5
1个回答
0
投票

尝试一下:

{% set sent_mails = 0 %}

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

{{ sent_mails }}
© www.soinside.com 2019 - 2024. All rights reserved.