在 "for" 上使用 "if" 条件

Using an "if" condition on "for"

我想将我的 symfony 4 应用程序投入生产,一切顺利,但在某些页面上出现错误 500,我可以找到问题的根源,但我不知道如何解决。

{% for articleUps in articleUp if articleUps.statut == "épinglé" %}
                    <div class="article">
                        <img src="{{ asset('assets/avatar/')~articleUps.user.image }}"
                             class="card-img-top photo rounded-circle">
                        <i class="fas fa-thumbtack"></i><a href="{{ path('forum_articles', {'id': articleUps.id , 'slug': articleUps.slug }) }}">{{ articleUps.sujet }}</a><br>
                        <strong>Crée le {{articleUps.createdAt|localizeddate('none', 'none', 'fr', null, 'EEEE d MMMM Y') }} • par<a href="{{ path('app_profil', {'username': articleUps.user.username}) }} ">{{ articleUps.user.username }}</a></strong>
                        {% if is_granted('ROLE_ADMIN') %}
                            <a href="{{ path('article_edit', {'id': articleUps.id}) }}" class="lien">editer</a>
                        {% endif %}
                    </div>
                    <hr>
                {% endfor %}

所以我在 symfony 日志中得到这个错误:

[2020-01-05 17:26:49] php.INFO: User Deprecated: Using an "if" condition on "for" tag in "forum/categories.html.twig" at line 91 is deprecated since Twig 2.10.0, use a "filter" filter or an "if" condition inside the "for" body instead (if your condition depends on a variable updated inside the loop). {"exception":"[object] (ErrorException(code: 0): User Deprecated: Using an \"if\" condition on \"for\" tag in \"forum/categories.html.twig\" at line 91 is deprecated since Twig 2.10.0, use a \"filter\" filter or an \"if\" condition inside the \"for\" body instead (if your condition depends on a variable updated inside the loop). at /var/www/html/ara.issoire-web.fr/vendor/twig/twig/src/TokenParser/ForTokenParser.php:46)"} []

我怎样才能使生产中不再有错误?

我改变了我的条件,这是在开发中仍然有效但在生产中无效的新闻:

{% for articleUps in articleUp %}
                        {% if articleUps.statut == "épinglé"  %}
                        <div class="article">
                            <img src="{{ asset('assets/avatar/')~articleUps.user.image }}"
                                 class="card-img-top photo rounded-circle">
                            <i class="fas fa-thumbtack"></i><a href="{{ path('forum_articles', {'id': articleUps.id , 'slug': articleUps.slug }) }}">{{ articleUps.sujet }}</a><br>
                            <strong>Crée le {{articleUps.createdAt|localizeddate('none', 'none', 'fr', null, 'EEEE d MMMM Y') }} • par<a href="{{ path('app_profil', {'username': articleUps.user.username}) }} ">{{ articleUps.user.username }}</a></strong>
                            {% if is_granted('ROLE_ADMIN') %}
                                <a href="{{ path('article_edit', {'id': articleUps.id}) }}" class="lien">editer</a>
                            {% endif %}
                        </div>
                        <hr>
                        {% endif %}
                    {% endfor %}

这只是一个警告 - Twig v2 没有弃用 for 语句中的 if,但它仍然可以正常工作,直到您升级到 Twig 的下一个主要版本。 Symfony 和相关工具非常擅长此类弃用。

至于确保您在生产中没有其他错误 - 日志(在开发和生产中)将显示哪里还有其他弃用问题或问题 - 启用更多日志记录非常有用,最好是'fingers-crossed' 错误处理程序。这会保留所有可用于请求的日志记录 - 但只有在出现错误时才将它们全部写入日志文件。

至于防患于未然——测试。单元测试、功能和集成。

As of Twig 2.10, use the filter filter instead, or an if condition inside the for body (if your condition depends on a variable updated inside the loop and you are not using the loop variable). - https://twig.symfony.com/doc/2.x/filters/filter.html