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.



seams that, even by code, you need to write XML
would be nice, for beginners, a “Create Layout Service for Magento”
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.
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.
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…
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 !!!
Seems like we are bored at the moment
Nice to hear some feedback from someone who knows their way around Magento.
This is not good idea to place layout code in action. How other guys will support it?
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();
Can we run this code from observer instead of controller?
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?
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.