Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register Twig Extension in Symfony

I want to use nochso/html-compress-twig extension to compress all html, inline css and js.But it is the first time that I register a new extension on Twig and I am bit confused about where I should add the following lines in my project:

$twig = new Twig_Environment($loader);
$twig->addExtension(new \nochso\HtmlCompressTwig\Extension());

I was reading the Twig's documentation but it didn't helped me much as they put the same example and just add the following:

Twig does not care where you save your extension on the filesystem, as all extensions must be registered explicitly to be available in your templates.

You can register an extension by using the addExtension() method on your main Environment object:

I only want enable the extension globally and be able to use {% htmlcompress %}{% endhtmlcompress %} in any twig template

like image 904
Kevin Gravell Avatar asked Dec 10 '22 09:12

Kevin Gravell


1 Answers

You can register your twig extension as a tagged service this way:

services:
    htmlcompress:
        class: '\nochso\HtmlCompressTwig\Extension'
        tags:
            - { name: twig.extension }
like image 178
Alain Tiemblo Avatar answered Dec 29 '22 00:12

Alain Tiemblo