简体   繁体   English

在 Symfony 中注册 Twig 扩展

[英]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:我想使用nochso/html-compress-twig 扩展来压缩所有 html、内联 css 和 js。但这是我第一次在 Twig 上注册一个新扩展,我对应该在哪里添加以下几行感到有些困惑我的项目:

$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 的文档,但它对我没有多大帮助,因为他们给出了相同的示例并添加了以下内容:

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我只想全局启用扩展并能够在任何树枝模板中使用{% htmlcompress %}{% endhtmlcompress %}

You can register your twig extension as a tagged service this way:您可以通过以下方式将您的树枝扩展注册为标记服务:

services:
    htmlcompress:
        class: '\nochso\HtmlCompressTwig\Extension'
        tags:
            - { name: twig.extension }

要启用 Twig 扩展,请将其添加为常规服务... http://symfony.com/doc/current/reference/dic_tags.html#twig-extension

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM