Twig 中不存在过滤器 "empty"
The filter "empty" does not exist at Twig
我正在将一个应用程序从 Symfony 2.0.15 迁移到 Symfony 2.7.0-BETA1,但出现此错误:
The filter "empty" does not exist in PDOneBundle::pdone.html.twig at line 1
这是 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';
}
}
这是 pdone.html.twig
的电话:
{% if form_action|empty == true or form_action|isnull == true or form_action|isset == false %} {% set form_action = '' %} {% endif %}
我在这里缺少什么?哪里出错了?
更新
我对我的代码进行了以下更改:
// 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'))
);
}
在 services.yml
中加载扩展:
services:
pdone.twig.extension:
class: GroupDCA\PDOneBundle\Extension\PDOneTwigExtension
tags:
- { name: twig.extension }
但仍然遇到同样的问题,还有其他想法吗?线索?我可以尝试其他方法吗?
更新 2
我已经解决了问题,但我得到的是:
Compile Error: Cannot use isset() on the result of an expression (you
can use "null !== expression" instead)
我不知道如何解决这个问题。我查看了堆栈跟踪,但我只得到了这个:
[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
有什么问题吗?任何人都可以帮我解决这个问题吗?
我不知道你的答案,但是 twig 有你可以使用的标准功能,请看这个例子:
{% if form_action is empty or form_action is null or form_action is not defined %}
{% set form_action = '' %}
{% endif %}
我正在将一个应用程序从 Symfony 2.0.15 迁移到 Symfony 2.7.0-BETA1,但出现此错误:
The filter "empty" does not exist in PDOneBundle::pdone.html.twig at line 1
这是 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';
}
}
这是 pdone.html.twig
的电话:
{% if form_action|empty == true or form_action|isnull == true or form_action|isset == false %} {% set form_action = '' %} {% endif %}
我在这里缺少什么?哪里出错了?
更新
我对我的代码进行了以下更改:
// 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'))
);
}
在 services.yml
中加载扩展:
services:
pdone.twig.extension:
class: GroupDCA\PDOneBundle\Extension\PDOneTwigExtension
tags:
- { name: twig.extension }
但仍然遇到同样的问题,还有其他想法吗?线索?我可以尝试其他方法吗?
更新 2
我已经解决了问题,但我得到的是:
Compile Error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
我不知道如何解决这个问题。我查看了堆栈跟踪,但我只得到了这个:
[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
有什么问题吗?任何人都可以帮我解决这个问题吗?
我不知道你的答案,但是 twig 有你可以使用的标准功能,请看这个例子:
{% if form_action is empty or form_action is null or form_action is not defined %}
{% set form_action = '' %}
{% endif %}