Magento Themes and Magento Extensions

Sick of Magento XML layouts?

Share

Haven’t wrote an article for a while, so here is a short one.

Are you sick of Magento XML layouts? If so, then do it all via the code, from the controller.

Here is how.

public function mycoolAction()
{
/* ... Some code ...*/
$update = $this->getLayout()->getUpdate();
/* ... Some code ...*/
$this->addActionLayoutHandles();
/* ... Some code ...*/
$this->loadLayoutUpdates();
/* ... Some code ...*/
/* My stuff, add pure XML like you do from XML layout files <img src="http://inchoo.net/wp-includes/images/smilies/icon_smile.gif" alt=":)">  */
$update->addUpdate('
<reference name="head">
<action method="addJs"><script>varien/product.js</script></action>
<action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params /><!--<if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/calendar.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/calendar-setup.js</name><!--<params/><if /><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>skin_js</type><name>js/bundle.js</name></action>
</reference>
');
/* ... Some code ...*/
$this->generateLayoutXml()->generateLayoutBlocks();
/* ... Some code ...*/
$this->renderLayout();
}

Why this in place of the “proper way” trough XML layout files? If you are to lazy to create layout file or you wish to reduce the number of your module files (possibly to keep all your module files in one directory).

Leave a Reply

You must be logged in to post a comment.