user2143356 user2143356 - 2 months ago 10
Twig Question

Trying to get this Twig (Symfony2) function extension to register

I'm getting an error:
"twig extension FatalErrorException: Error: Class Acme\Bundle\MyBundle\Twig not found in app/cache/dev/appDevDebugProjectContainer.php"

I've cleared the cache but this does nothing.

I'm just trying to test setting it up and then I can put in all my logic.

--

A file named MyTwigExtensions.php

namespace Acme\Bundle\MyBundle\Twig;

class MyTwigExtensions extends \Twig_Extension
{

public function getFunctions() {
return array(
new Twig_SimpleFunction('link', 'generate_link')
);
}

public function generate_link($params) {

return "THE-LINK-HERE";

}

public function getName() {

return "link";

}

}


In services.yml

services:
my_extension.twig.extension:
class: Acme\Bundle\MyBundle\Twig
arguments: []
tags:
- { name: twig.extension }

Answer

You have to enter fully qualified name of the extension class.

services:
  my_extension.twig.extension:
    class: Acme\Bundle\MyBundle\Twig\MyTwigExtensions # <--- here
    arguments: []
    tags:
      - { name: twig.extension }