Posts tagged layout
Posts tagged layout
Update: Turns out the previous post was missing a
<layout> tag in the code examples. This has been fixed, and we regret the error. (Thanks to Bartosz Cisek, proprietor of brillan.pl, for the correction)
I was surprised to discover I hadn’t covered this anywhere other than No Frills Magento Layout.
The XML files in a theme’s layout folder
are Layout Update XML files. They’re all loaded into a single file called the design package.
If you want to add your own XML file, you’ll need to add a Magento module. Once you’ve added a module to the system, you’ll need to configure the name of the Layout Update XML file you’d like added.
To do this, first add the following node to your module’s
<config> <!-- ... --> <frontend> <layout> <updates> </updates> </layout> </frontend> </config>
<updates> node is where you configure the XML files Magento will load the design package. Next, add a uniquely named node that identifies your update
<config> <frontend> <layout> <updates> <packagename_modulename> </packagename_modulename> </updates> </layout> </frontend> </config>
If you use a node name already in use by another Magento module (ex.
<catalog/>), one node will get lost in the shuffle. Finally, add a
<file/> node to specify which file you’d like loaded
<config> <frontend> <layout> <updates> <packagename_modulename> <file>packagename_modulename.xml</file> </packagename_modulename> </updates> </layout> </frontend> </config>
Magento will now look for a
packagename_modulename.xml and if found, include its updates in the design package.
If you need to add an XML update file to the admin console application, you’ll want everything located under the
<config> <adminhtml> <layout> <updates> <packagename_modulename> <file>packagename_modulename.xml</file> </packagename_modulename> </updates> </layout> </adminhtml> </config>
This one might be greek to people who haven’t read No Frills Magento Layout, but I’d like it spelled out so I don’t have to think about it again. (If you don’t own a copy, there are worse ways to learn Magento).
When Magento is loading handles from the Layout Update XML Files into the package layout, these updates are loaded into an internal array, handle by handle. All the handles from one file are loaded, and then handles from the next file are loaded. The
core.xml file is first, the
page.xml file is second, etc. This ordering will be significant.
More significant is the order of the handles.
default STORE_default foo_baz_index customer_logged_out
When generating the Page layout, the
Layout_Update object searches through the Package layout, by handle. This means (in the above example) all the
default handles are loaded first, then the
local.xml Layout Update XML File is always loaded last. That’s to ensure a store developer has a simple place to put all their layout updates that will ensure they’re loaded last and “win” over core layout updates. However, just because the updates in
local.xml are loaded last does not mean that any update added to
local.xml will run after an update in another file.
An update added with a
<default> handle in
local.xml will run before an update added with a
STORE_default handle in
page.xml is the first file loaded).
That’s because, irrespective of the file they’re in,
default handles are all added to the Page layout first, which means their meta-code will run first.