2013-09-24 36 views
0

我想操縱我index.html.twig日期的東西,如:TWIG/Symfony2的 - 過濾器 「date_modify」 不存在

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

並且得到錯誤:

The filter "date_modify" does not exist in XXX:YYY:index.html.twig at line 723

我使用的Symfony 2.0.16,以及所使用的日期是工作至今。

什麼能不存在樹枝庫中的過濾器的原因?

(Twig_Error_Syntax: The filter "date_modify" does not exist in "XXX:YYY:index.html.twig" at line 723 (uncaught exception) at /.../.../.../.../.../.../vendor/twig/lib/Twig/Node/Expression/Filter.php line 29)

+0

你正在使用哪種樹枝版本? – rohitcopyright

+0

剛剛發現,這是1.8.2 ..我wasn't能夠先找到它,所以我認爲it's一樣Symfony的版本.. –

回答

0

版本1.9.0中的新功能:date_modify過濾器已添加到Twig 1.9.0中。

也許你有一箇舊版本

0

創建你的樹枝擴展。在你的包中,創建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 
    } 
} 
?> 
+0

下面是有關創建您自己的擴展到相關的Symfony文檔的鏈接,如果有人需要關於這種方法的更多信息:http://symfony.com/doc/master/cookbook/templating/twig_extension.html – Sam