Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Twig Extension in controller

Tags:

slug

twig

symfony

I have a slugify method in an Twig Extension which i would like to use in some cases in a controller, f.e with redirects.
Is there an easy way for this?
How could i access functions from Twig Extensions in the controller?

Or do i have to make the slugify method somewere as a helper in order to use it in the code and in twig?

like image 425
ivoba Avatar asked Jul 11 '12 09:07

ivoba


People also ask

What is. twig extension?

File created by Twig, a PHP optimizing template engine; contains a template that will be generated into a specific final format, such as a HTML, JavaScript, XML, or CSS based file; the twig extension determines what engine should be used to create the final format whether it's the Twig or PHP engine.

How do I use twig extension in Drupal 8?

To start with it first, we need to create a module. 1.) Create a new module inside: /modules/custom/ directory inside your drupal project. description: 'Provides Twig Extension that process the Alt tag of any given image.


1 Answers

Access function / logic from twig and a controller

I think there are two solutions for this, both should use the Twig_Function_Method class.

1

The first solution gilden already posted, is to encapsulate the logic into a service and make a wrapper for the Twig Extension.

2

Another solution is to use only the Twig Extension. The Twig Extensino is already a service, you have to define it as service with the special <tag name="twig.extension" />. But it's also a service, which instance you can grab by the service container. And it's also possible to inject other services:

So you have your Twig Extension / Service:

class MyTwigExtension extends \Twig_Extension
{
    private $anotherService;

    public function __construct(SecurityService $anotherService= null)
    {
        $this->anotherService = $anotherService;
    }

    public function foo($param)
    {
       // do something
       $this->anotherService->bar($param);
    }


    public function getFunctions()
    {
        // function names in twig => function name in this calss
        return array(
            'foo' => new \Twig_Function_Method($this, 'foo'),
        );
    }

    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return 'my_extension';
    }
}

The services.xml looks like this

<service id="acme.my_extension" class="Acme\CoreBundle\Twig\Extension\MyTwigExtension">
        <tag name="twig.extension" />
        <argument type="service" id="another.service"></argument>
</service>

To acccess to the service from your controller you only have to use this:
$this->container->get('acme.my_extension')

Notice The only difference to a normal service is, that the twig extension is not lazy loaded (http://symfony.com/doc/current/cookbook/templating/twig_extension.html#register-an-extension-as-a-service)

like image 67
timaschew Avatar answered Sep 17 '22 13:09

timaschew