【问题标题】:TWIG/SYMFONY2 - The filter "date_modify" does not existTWIG/SYMFONY2 - 过滤器“date_modify”不存在
【发布时间】:2013-09-24 10:25:56
【问题描述】:

我试图在我的 index.html.twig 中操作一个日期,比如:

{{ myDate | date_modify("+3 day") | date('Y-m-d') }}

并得到错误:

过滤器“date_modify”不存在 XXX:YYY:index.html.twig 在第 723 行

我正在使用 Symfony 2.0.16,并且使用的日期到目前为止有效。

过滤器没有出现在 TWIG 库中的原因可能是什么?

(Twig_Error_Syntax: 过滤器“date_modify”不存在于 “XXX:YYY:index.html.twig”在第 723 行(未捕获的异常)在 /.../.../.../.../.../.../vendor/twig/lib/Twig/Node/Expression/Filter.php 第 29 行)

【问题讨论】:

  • 你用的是哪个twig版本?
  • 刚发现是1.8.2 ..我没能先找到它,所以我认为它与Symfony版本相同..

标签: php date symfony twig


【解决方案1】:

1.9.0 版中的新功能:在 Twig 1.9.0 中添加了 date_modify 过滤器。

可能你有一个旧版本

【讨论】:

    【解决方案2】:

    创建您的树枝扩展。在你的包中,创建Twig/Extension/XXXExtension.php

    <?php
    
    namespace XXX\YourBundle\Twig\Extension;
    
    use Symfony\Component\DependencyInjection\ContainerInterface;
    
    class XXXExtension extends \Twig_Extension
    {
        private $container;
    
        public function __construct(ContainerInterface $container)
        {
            $this->container = $container; 
        }
    
        public function getFilters()
        {
            return array('date_modify' => new \Twig_Filter_Method($this, 'dateModify', array('is_safe' => array('html'))));
        }
    
        public function dateModify($rangeDate)
        {
            // your code
        }
    }
    ?>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2015-09-17
    • 1970-01-01
    相关资源
    最近更新 更多