Symfony Twig Error: Unknown "^macro_name^" function. Did you mean "^similar_function_name^"?

Symfony Twig Error: Unknown "^macro_name^" function. Did you mean "^similar_function_name^"?

我做了以下树枝扩展:

class MyExtentions extends \Twig_Extension
{
   public function getFunctions()
   {
       return array(
           new \Twig_SimpleFunction('simiral_function_name', array($this, 'someFunction'),array('needs_environment' => true)),
       );
   }


   public function someFunction($url)
   {
       $url=parse_url($url,PHP_URL_PATH);
       $url=explode('/',$url);

       $params="";

       foreach($item as $key=> $url){
           $params.="_".$item;
       }

       return $params;
   }
}

在我的 Twig 模板上,我做了以下宏:

{% macro macro_name(url) %}
    {{ path('route_name',{'chunk':simiral_function_name(url)}) }}
{% endmacro   macro_name %}

但是当我访问我的模板时,我会访问它,例如:

<a href="{{macro_name("http://example.com/path/files/somefile")}}"></a>

我收到以下错误:

Unknown "macro_name" function. Did you mean "similar_function_name"?

当你想使用模板时,请按照说明进行操作there:

{% import _self as macro_template %}

之后您可以将其用作:

<a href="{{macro_template.macro_name("http://example.com/path/files/somefile")}}"></a>

代替macro_template你可以放任何你想要的。

将 {% endmacro article_route %} 更改为 {% endmacro macro_name %}

{% macro macro_name(url) %}
    {{ path('route_name',{'chunk':simiral_function_name(url)}) }}
{% endmacro macro_name %} 

更多详情Named Macro End-Tags