9

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

I want to minify all html output.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
timopeschka
  • 401
  • 3
  • 8
  • 16

5 Answers5

19

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   
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
5

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.

clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
4

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

Anton S
  • 12,750
  • 2
  • 35
  • 37
  • @Simon who says that you have to edit index.php for that , although you can do whatever you wan't as long as you are the code owner and take responsibility for any edits made – Anton S Sep 12 '17 at 11:36
  • Err... This is exactly what your answer suggests. And no, IMHO you should never edit the core. Never ever. – Simon Sep 12 '17 at 13:21
2

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

Joe Mastey
  • 26,809
  • 13
  • 80
  • 104
  • mod_pagespeed was made for this sort of situation. However it is still performing on every request. – clockworkgeek Dec 10 '10 at 16:32
  • That's very true, and I like the idea of taking advantage of the whole-page caching as a time saver. Do you have an idea on how much time the HTML deflation takes on mod_pagespeed? I don't have a sense for how significant of an issue this would be. – Joe Mastey Dec 10 '10 at 22:39
  • Because it's native code it must be faster than a scripted version. There are other caching options, mod_disk_cache and mod_mem_cache of course although I'm not sure if they're instantiated after mod_pagespeed, or you could use a reverse proxy. Those sorts of caching options generally require Expires and Vary headers which Magento doesn't set so their usefulness might be limited. Basically I don't know, only a debugging profile tool could tell you. – clockworkgeek Dec 10 '10 at 23:31
0

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

Binod Kalathil
  • 1,939
  • 1
  • 30
  • 43