Search code examples
phpsymfonytwigtwig-extension

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"?

Solution

  • 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.