[英]Symfony2 + Twig: Attempted to call function “replace” from the global namespace
I'm getting this error in my app and I can't find where I'm failing: 我的应用程序中出现此错误,但我找不到失败的地方:
Attempted to call function "replace" from the global namespace. 尝试从全局名称空间调用函数“替换”。
This is the Stacktrace: 这是Stacktrace:
[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
Now at my code I have this Twig Extension defined: 现在在我的代码中,我定义了这个Twig扩展:
services:
app.twig.extension:
class: AppBundle\Extension\AppTwigExtension
tags:
- { name: twig.extension }
And this is the 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';
}
}
What is wrong? 怎么了? I found this post but doesn't help in my case 我找到了这篇文章,但对我的情况没有帮助
From the error, it seems you've registered a filter called replace
, but you're trying to call it as a function. 从该错误看来,您似乎已经注册了一个名为replace
的过滤器,但是您试图将其作为函数调用。
So this should already work (but I don't think this is what you want to do): 因此,这应该已经可以工作了(但是我不认为这是您想要做的):
{{ my_variable|replace('something', 'with', 'this') }}
What I think you're trying to do is: 我认为您正在尝试做的是:
{{ replace(my_variable, 'replace', 'this') }}
To register a function, add a method called getFunctions
to your AppTwigExtension
class, and move the replace
definition to that. 若要注册函数,请在AppTwigExtension
类中添加一个名为getFunctions
的方法,然后将replace
定义AppTwigExtension
该类。 See documentation for more details. 有关更多详细信息,请参见文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.