Skip to content

Instantly share code, notes, and snippets.

@herveguetin
Last active January 13, 2017 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save herveguetin/6e016a7b90196d36891f to your computer and use it in GitHub Desktop.
Save herveguetin/6e016a7b90196d36891f to your computer and use it in GitHub Desktop.
Get HTML for a standalone layout in Magento
<?php
/**
* Retrieve content of a standalone layout
*
* @return string
* @throws Mage_Core_Exception
*/
public function getStandaloneLayoutHtml()
{
$layout = Mage::getModel('core/layout'); // Instanciate a brand new layout (not using the singleton from Mage::app()->getLayout())
$layout->setArea('frontend'); // Set the area for the layout (frontend or adminhtml)
$update = $layout->getUpdate(); // Get the layout update object
$update->load(array('update_handle_1', 'update_handle_2')); // Load update handles (array | string)
$layout->generateXml();
$layout->generateBlocks();
return $layout->getOutput();
}
?>
@herveguetin
Copy link
Author

In some situations, you may want to create a layout based on very specific handles. Using the $layout singleton of the current page is then of no use and may lead to bugs. The solution is to create a new standalone layout object in which you can inject your preferred layout handles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment