Magento

Better way to load layout blocks outside of Magento

For loading Magento Layout Blocks externally or in a controller without any complexity, use this:

From external file, we need to initiate Magento session:

Mage::getSingleton(‘core/session’, array(‘name’=>’frontend’));
$session = Mage::getSingleton(‘customer/session’, array(‘name’=>’frontend’));
$layout = Mage::app()->getLayout();
$layout->getUpdate()->addHandle(‘default’)->load();
$layout->generateXml()->generateBlocks();
$top = $layout->getBlock(‘top.links’)->toHtml();
echo $top;

From internal controller:

$layout = Mage::app()->getLayout();
$layout->getUpdate()->addHandle(‘default’)->load();
$layout->generateXml()->generateBlocks();
$top = $layout->getBlock(‘top.links’)->toHtml();
echo $top;

You Might Also Like

  • Ephy

    Good to know.
    This works fine but there is a problem when getting blocks from an external file, the block is not translated from the store language settings. Any idea ?