How layout is rendered in magento
devm.com.np
<?php 
public function viewAction()
{
    
$this->loadLayout();
    
$this->renderLayout();        
}

//Mage_Core_Controller_Varien_Action
public function loadLayout()
{
    
// add default layout handles for this action
    
$this->addActionLayoutHandles();
    
$this->loadLayoutUpdates();
    
$this->generateLayoutXml();
    
$this->generateLayoutBlocks();
}

public function 
addActionLayoutHandles()
{
    
//$this->getLayout() is singleton

    
$update $this->getLayout()->getUpdate();

    
// load action handle
    
$update->addHandle(strtolower($this->getFullActionName()));
}

public function 
loadLayoutUpdates()
{
    
$this->getLayout()->getUpdate()->load();
}

//Mage_Core_Model_Layout_Update
public function load($handles=array())
{
    foreach (
$this->getHandles() as $handle) {
        
$this->merge($handle);
    }

    
$this->saveCache();
}

public function 
merge($handle)
{
    
$this->fetchPackageLayoutUpdates($handle);
}

public function 
fetchPackageLayoutUpdates($handle)
{
    
$this->fetchFileLayoutUpdates();

    foreach (
$this->_packageLayout->$handle as $updateXml) {
        
$this->fetchRecursiveUpdates($updateXml);
        
$this->addUpdate($updateXml->innerXml());
    }
}

public function 
fetchFileLayoutUpdates()
{
    
$this->_packageLayout $this->getFileLayoutUpdatesXml();
}
 
public function 
getFileLayoutUpdatesXml($area$package$theme$storeId null)
{
    foreach (
$updateFiles as $file) {
        
$filename $design->getLayoutFilename($file);
        
$fileStr file_get_contents($filename);
        
$fileXml simplexml_load_string($fileStr$elementClass);
    
        
$layoutStr .= $fileXml->innerXml();
    }
    
    
$layoutXml simplexml_load_string('<layouts>'.$layoutStr.'</layouts>'$elementClass);
}
//Mage_Core_Controller_Varien_Action
public function generateLayoutXml()
{
     
$this->getLayout()->generateXml();
}

public function 
generateLayoutBlocks()
{
    
$this->getLayout()->generateBlocks();
}

//Mage_Core_Model_Layout
public function generateBlocks($parent=null)
{
    foreach (
$parent as $node) {
        
this->_generateBlock($node$parent);
        
$this->generateBlocks($node);
    }
}

 protected function 
_generateBlock($node$parent)
{
    
$className = (string)$node['type'];
    
    
$block $this->addBlock($className$blockName);
    
    
$parentName $parent->getBlockName();
    
$parentBlock $this->getBlock($parentName);

    if (!empty(
$parentBlock)) {
        
$parentBlock->insert($block$siblingtrue$alias);
    }
    if (!empty(
$node['template'])) {
        
$block->setTemplate((string)$node['template']);
    }
}
//Mage_Core_Controller_Varien_Action
public function renderLayout($output='')
{
    
$output $this->getLayout()->getOutput();
    
$this->getResponse()->appendBody($output);
}

//Mage_Core_Model_Layout
public function getOutput()
{
    
$out '';
    if (!empty(
$this->_output)) {
        foreach (
$this->_output as $callback) {
            
// $callback[0] is root and $callback[1] is toHtml(),all other blocks are children of root        
            
$out .= $this->getBlock($callback[0])->$callback[1]();
        }
    }
}

//Mage_Core_Block_Abstract
final public function toHtml()
{
    
$html $this->_toHtml();    
}

//Mage_Core_Block_Template
protected function _toHtml()
{
    
$html $this->renderView();
}

public function 
renderView()
{
    
$html $this->fetchView($this->getTemplateFile());
}

public function 
fetchView($fileName)
{
    include 
$this->_viewDir DS $fileName;   
}
?>