简体   繁体   English

如何在 Symfony2 中压缩 html output?

[英]How to compress html output in Symfony2?

I know Twig has a {% spaceless %} filter, but it only removes spaces between html tags.我知道 Twig 有一个 {% spaceless %} 过滤器,但它只删除 html 标签之间的空格。

I need to return a single line of html for every page in the site.我需要为站点中的每个页面返回一行 html。 I have a regexp that can deal with that, but I don't know where to use it in sf2, since everything happens automagically.我有一个可以处理这个问题的正则表达式,但我不知道在 sf2 中在哪里使用它,因为一切都是自动发生的。

I think I have to register a new templating engine, or add a twig extension, but I couldn't find enough documentation about the subject, so I'm stuck我想我必须注册一个新的模板引擎,或者添加一个 twig 扩展,但是我找不到足够的关于这个主题的文档,所以我被卡住了

Any ideas?有任何想法吗?

To run your regex on all view output, you can hook into the kernel.response event that is dispatched by the Symfony2 framework.要在所有视图 output 上运行正则表达式,您可以挂钩到由 Symfony2 框架调度的kernel.response事件。

From the section on kernel.response :kernel.response部分:

The purpose of this event is to allow other systems to modify or replace the Response object after its creation:此事件的目的是允许其他系统在创建后修改或替换响应 object:

public function onKernelResponse(FilterResponseEvent $event)
{
    $response = $event->getResponse();

    // ... modify the response object
}

I would recommend reading the Internals chapter for even more details.我建议阅读内部章节以了解更多详细信息。

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

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