简体   繁体   English

Symfony2 + Twig:尝试从全局名称空间调用函数“ replace”

[英]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.

相关问题 尝试从全局名称空间Symfony2 + Ekino Wordpress调用函数“ wp” - Attempted to call function “wp” from the global namespace Symfony2 + Ekino Wordpress 尝试从Silex的全局名称空间调用函数 - Attempted to call function from the global namespace on Silex 尝试从名称空间symfony Controller调用函数 - Attempted to call function from namespace symfony Controller Symfony imap“试图从全局名称空间中调用函数“ imap_open”。 - Symfony imap “Attempted to call function ”imap_open“ from the global namespace.” Symfony2尝试从全局名称空间加载类。 您是否忘记了“使用”声明? - Symfony2 Attempted to load class from the global namespace. Did you forget a “use” statement? 试图从全局命名空间调用函数“yaml_parse_file” - Attempted to call function "yaml_parse_file" from the global namespace 如何在Symfony2的树枝中调用函数? - How to call function in twig in Symfony2? 尝试从名称空间-Symfony2加载类“ COM” - Attempted to load class “COM” from namespace -Symfony2 symfony2:尝试从名称空间加载类“ Memcache” - symfony2: Attempted to load class “Memcache” from namespace Symfony2:如何用全局Twig变量替换某个字符串? - Symfony2: How to replace a certain string with a global Twig variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM