在Symfony中注册Twig扩展名 [英] Register Twig Extension in Symfony
问题描述
我想使用 nochso/html-compress-twig扩展压缩所有html,内联css和js.但这是我第一次在Twig上注册新的扩展名,我对在项目中的以下行添加位置感到困惑:
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());
我正在阅读Twig的文档,但对他们没有什么帮助,因为他们提出了相同的示例,只是添加了以下内容:
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:
我只想全局启用扩展,并且能够在任何树枝模板中使用{% htmlcompress %}{% endhtmlcompress %}
I only want enable the extension globally and be able to use {% htmlcompress %}{% endhtmlcompress %}
in any twig template
推荐答案
您可以通过以下方式将树枝扩展注册为带标签的服务:
You can register your twig extension as a tagged service this way:
services:
htmlcompress:
class: '\nochso\HtmlCompressTwig\Extension'
tags:
- { name: twig.extension }
这篇关于在Symfony中注册Twig扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!