duanmangxie7131 2015-04-13 16:57
浏览 34
已采纳

Twig中不存在过滤器“empty”

I'm migrating an app from Symfony 2.0.15 to Symfony 2.7.0-BETA1 and I'm getting this error:

The filter "empty" does not exist in PDOneBundle::pdone.html.twig at line 1

This is the code for Extension\PDOneTwigExtension:

class PDOneTwigExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            'var_dump'   => new \Twig_Filter_Function('var_dump'),
            'empty'  => new \Twig_Filter_Method($this, 'is_empty'),
            'isset'  => new \Twig_Filter_Method($this, 'is_set'),
            'isnull'  => new \Twig_Filter_Method($this, 'is_null'),
            'ucfirst'  => new \Twig_Filter_Method($this, 'uc_first'),
            'ucwords'  => new \Twig_Filter_Method($this, 'uc_words'),
            'count'  => new \Twig_Filter_Method($this, 'co_unt'),
            'sizeof'  => new \Twig_Filter_Method($this, 'size_of'),
            'concat'  => new \Twig_Filter_Method($this, 'concat'),
            'in_array'  => new \Twig_Filter_Method($this, 'inarray'),
            'array'  => new \Twig_Filter_Method($this, 'array_'),
            'add_to_array'  => new \Twig_Filter_Method($this, 'add_to_array'),
            'replace'  => new \Twig_Filter_Method($this, 'replace'),
            'htmlentitydecode'  => new \Twig_Filter_Method($this, 'htmlentitydecode'),
        );
    }

    public function is_empty($sentence)
    {
        return empty($sentence);
    }

    // others methods goes here

    public function getName()
    {
        return 'pdone_twig_extension';
    }
}

And this is the call at pdone.html.twig:

{% if form_action|empty == true or form_action|isnull == true or form_action|isset == false %} {% set form_action = '' %} {% endif %}

What I'm missing here? Where is the error?

UPDATE

I've made the following changes at my code:

// Changes for use Twig_SimpleFilter() insted of Twig_Filter_Method()
public function getFilters()
{
    return array(
        'var_dump'   => new \Twig_Filter_Function('var_dump'),
        new Twig_SimpleFilter('empty', array($this, 'is_empty')),
        new Twig_SimpleFilter('isset', array($this, 'is_set')),
        new Twig_SimpleFilter('isnull', array($this, 'is_null')),
        new Twig_SimpleFilter('ucfirst', array($this, 'uc_first')),
        new Twig_SimpleFilter('ucwords', array($this, 'uc_words')),
        new Twig_SimpleFilter('count', array($this, 'co_unt')),
        new Twig_SimpleFilter('sizeof', array($this, 'size_of')),
        new Twig_SimpleFilter('concat', array($this, 'concat')),
        new Twig_SimpleFilter('in_array', array($this, 'inarray')),
        new Twig_SimpleFilter('array', array($this, 'array_')),
        new Twig_SimpleFilter('add_to_array', array($this, 'add_to_array')),
        new Twig_SimpleFilter('replace', array($this, 'replace')),
        new Twig_SimpleFilter('htmlentitydecode', array($this, 'htmlentitydecode'))
    );
}

Loads extension in services.yml:

services:
    pdone.twig.extension:
        class: GroupDCA\PDOneBundle\Extension\PDOneTwigExtension
        tags:
            -  { name: twig.extension }

But still getting the same issue, any other idea? A clue? Something else I can try?

Update 2

I got the issue fixed but I'm getting this:

Compile Error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)

I don't know how to fix this one. I take a look to the stacktrace but I got only this:

[1] Symfony\Component\Debug\Exception\FatalErrorException: Compile Error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
    at n/a
        in /var/www/html//reptooln_admin/app/cache/dev/twig/68/7f/63589dd3687cb849dd68e6b6c10aa99eda1d82f95a5f3ac52a864d200499.php line 39

What could be wrong? Can any give me some help on this one?

  • 写回答

1条回答 默认 最新

  • duanpu1963 2015-04-13 18:18
    关注

    i dont know your answer, but twig have standard function that you can use them, see this example:

    {% if form_action is empty or form_action is null or form_action is not defined %}
        {% set form_action = '' %}
    {% endif %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法