Magento:缩小HTML输出?

tim*_*hka 9 html templates minify magento

magento中是否有任何文件输出所有html?

我想缩小所有html输出.

Ala*_*orm 19

Magento使用响应对象发送所有输出.

将所有输出添加到此对象,然后sendResponse调用其方法.

如果要更改输出,请为http_response_send_before事件设置侦听器

<!-- in your module's config.xml -->
<http_response_send_before>
    <observers>
        <unique_name>
            <type>singleton</type>
            <class>group/observer</class>
            <method>alterOutput</method>
        </unique_name>
    </observers>
</http_response_send_before>
Run Code Online (Sandbox Code Playgroud)

然后在你的观察者中你可以得到并设置身体

class Packagename_Modulename_Model_Observer
{
    public function alterOutput($observer)
    {
        $response = $observer->getResponse();       
        $html     = $response->getBody();           
        //modify html here          
        $response->setBody($html);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您有兴趣,可以在sendResponse以下类的方法中调用此事件

app/code/core/Mage/Core/Controller/Response/Http.php
Run Code Online (Sandbox Code Playgroud)

并且输出本身是在sendResponseoutputBody方法中发送的

lib/Zend/Controller/Response/Abstract.php   
Run Code Online (Sandbox Code Playgroud)


clo*_*eek 5

理想情况下,您希望在缓存输出之前执行缩小以避免过于频繁地执行此操作.我能想到的最好的地方是覆盖Mage_Page_Block_Html并将以下函数添加到新类中:

protected function _toHtml()
{
    $html = parent::_toHtml();
    // MINIFY CONTENTS OF $html HERE
    return $html;
}
Run Code Online (Sandbox Code Playgroud)

这样,它对整个页面执行一次操作,然后Magento以通常的方式缓存返回的值.它不是单独执行每个块,可能效率较低.


Ant*_*n S 4

您始终可以使用 ob 函数来获取 index.php 中的输出,然后根据需要处理内容。但我怀疑它是否会像启用 gzip 或 deflate 一样增强您的网站