Magento: Minify HTML Output?

2020-02-26 09:11发布

Is there any file in magento where all html will be output?

I want to minify all html output.

5条回答
家丑人穷心不美
2楼-- · 2020-02-26 09:45

Maybe mod_pagespeed from Google? That would do it transparently for you. +1 for gzip and deflate either way.

查看更多
劳资没心,怎么记你
3楼-- · 2020-02-26 09:47

Maybe someone coming here might find this Magento extension helpful: http://www.magentocommerce.com/magento-connect/html-minify-by-jemoon.html

查看更多
The star\"
4楼-- · 2020-02-26 09:53

you can always use ob functions to get the output in index.php and then do with the content whatever you need. but i doubt if it will boost your site as much as enabling gzip or deflate

查看更多
等我变得足够好
5楼-- · 2020-02-26 10:00

Magento uses a response object to send all output.

All output is added to this object, and then its sendResponse method is called.

If you want to alter the output, setup a listener for the http_response_send_before event

<!-- 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>

And then in your observer you may get and set the body

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

If you're interested, this event is called in the sendResponse method of the following class

app/code/core/Mage/Core/Controller/Response/Http.php

and the output itself is sent in the sendResponse and outputBody methods of

lib/Zend/Controller/Response/Abstract.php   
查看更多
▲ chillily
6楼-- · 2020-02-26 10:11

Ideally you want to perform minification before the output is cached to avoid doing it too often. The best place I can think of is by overriding Mage_Page_Block_Html and adding the following function to your new class:

protected function _toHtml()
{
    $html = parent::_toHtml();
    // MINIFY CONTENTS OF $html HERE
    return $html;
}

This way it performs the action once for the whole page, the returned value may then be cached by Magento in it's usual manner. It's not performing on each block individually which might be less efficient.

查看更多
登录 后发表回答