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 ...*/
$this->addActionLayoutHandles();
/* ... Some code ...*/
$this->loadLayoutUpdates();
/* ... Some code ...*/
/* My stuff, add pure XML like you do from XML layout files :) */
$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).
Cheers.
15 comments
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.
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 ?
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!
hello
i want to move blocks in frontend in magento
so how to do this
and how to update xml file on moving
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.
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?
Can we run this code from observer instead of controller?
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');
$headBlock->addJs('varien/product.js')
->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');
$this->renderLayout();
This is not good idea to place layout code in action. How other guys will support it?
Seems like we are bored at the moment 🙂
Nice to hear some feedback from someone who knows their way around Magento.
I got the point Branko, but, let me fool around a little bit:
“If you are to lazy…”
if you are, you would never develop for Magento !!! 🙂
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…
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.
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.
seams that, even by code, you need to write XML 😀
would be nice, for beginners, a “Create Layout Service for Magento” 🙂