Magento Themes and Magento Extensions

Sick of Magento XML layouts?


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 ...*/
/* ... Some code ...*/
/* ... Some code ...*/
/* My stuff, add pure XML like you do from XML layout files <img src="" alt=":)">  */
<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>
/* ... Some code ...*/
/* ... Some code ...*/

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.