twig - 在“for”上使用“if”条件
问题描述
我想将我的 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 没有弃用if
onfor
声明,但它仍然可以正常工作,直到您升级到 Twig 的下一个主要版本。Symfony 和相关工具非常适合此类弃用。
至于确保您在生产中没有其他错误 - 日志(在开发和生产中)将显示存在其他弃用问题或问题的地方 - 启用更多日志记录非常有用,理想情况下使用“手指交叉” ' 错误处理程序。这会保留所有可用于请求的日志记录 - 但仅在出现错误时将它们全部写入日志文件。
至于在问题存在之前发现问题 - 测试。单元测试、功能和集成。
从 Twig 2.10 开始,请改用 filter 过滤器,或者在 for 主体中使用 if 条件(如果您的条件取决于循环内更新的变量并且您没有使用循环变量)。- https://twig.symfony.com/doc/2.x/filters/filter.html
推荐阅读
- reactjs - new Date() 总是返回本地日期....如何在 UTC 中获取 Date 对象?
- selenium - 在 selenium 中究竟是如何动态处理implicitWait 的?
- kubernetes - 如何使用 AKS 挂载本地卷 hostPath?
- python - Why pytorch model cannot recognize the tensors I defined?
- python - NetworkX g.neighbors(n) dict_keyiterator error message
- android - 通过设置为纵向模式的 xcode 在我的物理 iPhone 上启动颤振应用程序有效,但在通过 android studio 启动时无效
- android - 任务管理器的颤振推送路线?
- html - Internet Explorer 上的自定义范围滑块问题
- list - 从 Prolog 的列表中删除括号
- list - The argument type 'Iterable>' can't be assigned to the parameter type 'List'