Magento Design Output Rendering

In this blog post we will see how magento renders output from blocks and phtml files.
In the previous blog post we saw how magento loads all layout xml files, blocks and phtml files. In this blog post we will see how magento outputs it to the browser


The function which we call in our controller files to send output is renderLayout(). This function is defined in “Mage_Core_Controller_Varien_Action”

The main code in this function is



        $output = $this->getLayout()->getOutput();

The code “setDirectOutput(false)” basically turns on “ob_start()” (output buffering). So because of this direct output to browser is turned off and flushing of output buffer only happens when we manually “echo” the output. Next lets look at the “getOutput()” function in “Mage_Core_More_Layout” class.

      public function getOutput() {
        $out = '';
        if (!empty($this->_output)) {
            foreach ($this->_output as $callback) {
                $out .= $this->getBlock($callback[0])->$callback[1]();

        return $out;

This function does a foreach loop on “$this->_output”, the _output array contains references to blocks which have “output” attribute set in them. e.g

<block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">

so block “root” has output=”toHtml” set in it.
This block is called by the “getOutput()” function, the toHtml() function is called on the “root” block.
The “toHtml()” function is defined in the class “Mage_Page_Block_Abstract” class. This function first checks loaded html in cache, if it exists directly reads from cache else calls the “_toHtml()” function.
The “_toHtml()” is defined in “Mage_Page_Block_Template” class, and this function loads the template file and simply “includes” it. Including the template file causes all other child blocks/templates also to be included and hence the entire html output is generated.

Next in the “renderLayout()” function we have


The “getResponse()” object is an instance of class “Mage_Core_Controller_Response_Http”

So till this step we have generated all html and set it in the response object.

Next in our “Mage_Core_Controller_Varien_Front” class, the “dispatch()” function the last line which we have is


This function simply send any headers which were set previously and then outputs (echo) the content.