Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10028

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

I have made the following twig extention:

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;
   }
}

And over my Twig Template I did the following macro:

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

But when over my template I access it eg:

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

I get the following error:

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

Upvotes: 2

Views: 1545

Answers (2)

habibun
habibun

Reputation: 1630

instead of {% endmacro article_route %} change to {% endmacro macro_name %}

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

more details Named Macro End-Tags

Upvotes: 1

Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10028

Just when you want to use the template do as described there:

{% import _self as macro_template %}

Afterwards you can use it as:

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

In place of macro_template you can put whatever you want.

Upvotes: 2

Related Questions