Symfony - Twig - 过滤"localizeddate"在DateTime上不起作用

Zag*_*loo 10 symfony twig

我正在使用树枝,当我应用过滤器时

{{tutoriel.dateCreation | localizeddate('medium', 'none') }} 在我的日期时间,我有这个错误:

The filter "localizeddate" does not exist in Video2LearnAdministrationBundle:VisualisationFicheTutoriel:fiche_tutoriel.html.twig at line 167
Run Code Online (Sandbox Code Playgroud)

哪里我错了?

谢谢 :)

Zag*_*loo 23

我找到了解决方案.

您应该安装twig intl扩展.如果没有,做composer require twig/extensions

您可以像这样激活config.yml或services.yml文件中的服务:

services:
    twig.extension.intl:
        class: Twig_Extensions_Extension_Intl
        tags:
            - { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)

  • 在 Symfony 4 中,执行 `composer require twig/extensions`,然后转到 `config/packages/twig_extensions.yml`,然后取消注释 Intl 行 (5认同)
  • 自 2020 年起,该软件包已被废弃。建议像 Breaith 的回答所说的那样使用 twig/intl-extra 。 (2认同)

Bre*_*ith 8

我使用新的树枝过滤器format_date()https ://twig.symfony.com/doc/2.x/filters/format_date.html

  1. 安装:composer req twig/intl-extra
  2. 在树枝中的用法:
created_at|format_date('full')
created_at|format_date('full', '', null, 'gregorian', 'fr')
created_at|format_date('none', 'MMMM Y', null, 'gregorian', 'fr')
Run Code Online (Sandbox Code Playgroud)