Sick of Magento XML layouts?

Featured Image

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 🙂 */
		<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).



  1. Sometime we need to insert extra head tags like meta or link dynamically which is not possible from layout xml file. It would be great help to start up for the solution of such problem. So THUMBS UP Branko!! 🙂 for this tips.

  2. Very nice tutorial…I am trying to update xml using extension I have used event observer “controller_action_layout_load_before” to update layout which give me “Mage_Core_Model_Layout” object…can you please show how can we update xml with this object ?

  3. Wow! this is pretty good, must say, magento always impresses . you can do your task in many ways, but it requires steady experience and know how. Thumbs up for this article. inchoo people are good, must appreciate for your efforts!

  4. Exactly what I am looking for this is very useful when we need to deal with sites having multiple themes and we want to deliver module that works without knowing which theme is currently active.

  5. I would like to add a css to the admin backend. But can’t seem to get the right controller. Does anybody have an example?

  6. If you are going to do it through code, you should do it like this, which makes very much sense since these are the methods you request through the action tag.

    $headBlock = $this->getLayout()->getBlock('head');
    ->addItem('js_css', 'calendar/calendar-win2k-1.css', null, null, 'can_load_calendar_js')
    ->addItem('js', 'calendar/calendar.js', null, null, 'can_load_calendar_js')
    ->addItem('js', 'calendar/calendar-setup.js', null, null, 'can_load_calendar_js')
    ->addItem('skin_js', 'js/bundle.js');

  7. The point in my article was not “this is how it should be done”, it was “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)”.

    No specific reason… its just another possible way…

  8. Why would you want to that, when it can be easily done from an XML file. I mean, if you skipped writing the XML, ok, it would’ve been nice. But since you still have to use XML, the proper way of doing it is via layout files. Layout updates are slower than regular layout rendering, as far as I know. Plus, I don’t think they’re being cached.

  9. Lol… good point.

    Article title is a bit misleading. My point was to show how to do it trough code and avoid adding extra file to your module.

    Thanks for feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.