I'm quite new to Magento, and i'm looking into the following, wondering if it's possible.

at some point magento will need to do some echo'ing to the client, ie. stream it's html data to the client. At what point does this happen? Along its execution, or is the output gathered and sent at the end?

I'm looking into a way of inspecting/modifying the html of a page in a generic way, so not from inside a template or something.

Is this possible?


You can use the event controller_action_layout_render_before if you want to alter the html for all the pages.
If you want it for a specific page page use the event controller_action_layout_render_before_[module_name]_[controller_name]_[action_name].
For example for the product view page the event is controller_action_layout_render_before_catalog_product_view, for the cart page is controller_action_layout_render_before_checkout_cart_index. Inside your observer you can access the html like this:

$html = Mage::app()->getLayout()->getOutput();

and you can change it using

  • 1
    I s output buffering always on for layouts? I remember some if-clause in there, so perhaps this requires opening another output buffer first under some circumstances, doesn't it? – hakre Jul 29 '15 at 17:37

...at some point magento will need to ... stream it's html data to the client. At what point does this happen?

See Mage_Core_Controller_Varien_Front::dispatch() - there's a code comment which answers your question directly:

public function dispatch()

    // This event gives possibility to launch something before sending output (allow cookie setting)
    Mage::dispatchEvent('controller_front_send_response_before', array('front'=>$this));
    Mage::dispatchEvent('controller_front_send_response_after', array('front'=>$this));
    return $this;
  • 2
    oh man...how did I miss this? This is way better than my solution if you want to manipulate the html for all pages. – Marius Jul 29 '15 at 15:30
  • 2
    @Marius I can send you a discount for the Magento U Fundamentals course ;-) – benmarks Jul 29 '15 at 15:48
  • 2
    Make it 100% discount and I'll go. – Marius Jul 29 '15 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.