Symfony2 + Twig:尝试从全局命名空间调用函数 "replace"
Symfony2 + Twig: Attempted to call function "replace" from the global namespace
我在我的应用程序中遇到此错误,但我找不到失败的地方:
Attempted to call function "replace" from the global namespace.
这是堆栈跟踪:
[1] Symfony\Component\Debug\Exception\UndefinedFunctionException: Attempted to call function "replace" from the global namespace.
at n/a
in /var/www/html/reptooln_admin/app/cache/dev/twig/eb/76/c3cb3f071f775598b83974700b4a2523941a76b0f3cf8801d01d9210eae0.php line 318
现在在我的代码中定义了这个 Twig 扩展:
services:
app.twig.extension:
class: AppBundle\Extension\AppTwigExtension
tags:
- { name: twig.extension }
这是 class:
<?php
namespace AppBundle\Extension;
class AppTwigExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
'var_dump' => new \Twig_Filter_Function('var_dump'),
'empty' => new \Twig_Filter_Function('empty', array($this, 'is_empty')),
'isset' => new \Twig_Filter_Function('isset', array($this, 'is_set')),
'isnull' => new \Twig_Filter_Function('isnull', array($this, 'is_null')),
'ucfirst' => new \Twig_Filter_Function('ucfirst', array($this, 'uc_first')),
'ucwords' => new \Twig_Filter_Function('ucwords', array($this, 'uc_words')),
'count' => new \Twig_Filter_Function('count', array($this, 'co_unt')),
'sizeof' => new \Twig_Filter_Function('sizeof', array($this, 'size_of')),
'concat' => new \Twig_Filter_Function('concat', array($this, 'concat')),
'in_array' => new \Twig_Filter_Function('in_array', array($this, 'inarray')),
'array' => new \Twig_Filter_Function('array', array($this, 'array_')),
'add_to_array' => new \Twig_Filter_Function('add_to_array', array($this, 'add_to_array')),
'replace' => new \Twig_Filter_Function('replace', array($this, 'replace')),
'htmlentitydecode' => new \Twig_Filter_Function('htmlentitydecode', array($this, 'htmlentitydecode')),
);
}
public function replace($subject, $search, $replace)
{
return str_replace($search, $replace, $subject);
}
// functions goes here
public function getName()
{
return 'app_twig_extension';
}
}
怎么了?我找到了 this post 但对我的情况没有帮助
从错误来看,您似乎注册了一个名为 replace
的过滤器,但您正试图将其作为函数调用。
所以这应该已经起作用了(但我不认为这是你想要做的):
{{ my_variable|replace('something', 'with', 'this') }}
我认为你想做的是:
{{ replace(my_variable, 'replace', 'this') }}
要注册函数,请将名为 getFunctions
的方法添加到您的 AppTwigExtension
class,并将 replace
定义移至该方法。有关详细信息,请参阅 documentation。
我在我的应用程序中遇到此错误,但我找不到失败的地方:
Attempted to call function "replace" from the global namespace.
这是堆栈跟踪:
[1] Symfony\Component\Debug\Exception\UndefinedFunctionException: Attempted to call function "replace" from the global namespace.
at n/a
in /var/www/html/reptooln_admin/app/cache/dev/twig/eb/76/c3cb3f071f775598b83974700b4a2523941a76b0f3cf8801d01d9210eae0.php line 318
现在在我的代码中定义了这个 Twig 扩展:
services:
app.twig.extension:
class: AppBundle\Extension\AppTwigExtension
tags:
- { name: twig.extension }
这是 class:
<?php
namespace AppBundle\Extension;
class AppTwigExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
'var_dump' => new \Twig_Filter_Function('var_dump'),
'empty' => new \Twig_Filter_Function('empty', array($this, 'is_empty')),
'isset' => new \Twig_Filter_Function('isset', array($this, 'is_set')),
'isnull' => new \Twig_Filter_Function('isnull', array($this, 'is_null')),
'ucfirst' => new \Twig_Filter_Function('ucfirst', array($this, 'uc_first')),
'ucwords' => new \Twig_Filter_Function('ucwords', array($this, 'uc_words')),
'count' => new \Twig_Filter_Function('count', array($this, 'co_unt')),
'sizeof' => new \Twig_Filter_Function('sizeof', array($this, 'size_of')),
'concat' => new \Twig_Filter_Function('concat', array($this, 'concat')),
'in_array' => new \Twig_Filter_Function('in_array', array($this, 'inarray')),
'array' => new \Twig_Filter_Function('array', array($this, 'array_')),
'add_to_array' => new \Twig_Filter_Function('add_to_array', array($this, 'add_to_array')),
'replace' => new \Twig_Filter_Function('replace', array($this, 'replace')),
'htmlentitydecode' => new \Twig_Filter_Function('htmlentitydecode', array($this, 'htmlentitydecode')),
);
}
public function replace($subject, $search, $replace)
{
return str_replace($search, $replace, $subject);
}
// functions goes here
public function getName()
{
return 'app_twig_extension';
}
}
怎么了?我找到了 this post 但对我的情况没有帮助
从错误来看,您似乎注册了一个名为 replace
的过滤器,但您正试图将其作为函数调用。
所以这应该已经起作用了(但我不认为这是你想要做的):
{{ my_variable|replace('something', 'with', 'this') }}
我认为你想做的是:
{{ replace(my_variable, 'replace', 'this') }}
要注册函数,请将名为 getFunctions
的方法添加到您的 AppTwigExtension
class,并将 replace
定义移至该方法。有关详细信息,请参阅 documentation。