2

I think I read somewhere that the best place to add a layout handle for a custom module was through the local.xml file in layouts folder. However, almost all modules that I have seen, never has one used the local.xml. Is it the best practice to use local.xml?

Do you have a link to an article explaining this practice? Also, say I just want to add a lone js/css file to the layout on every page, would local.xml's default handle be the best practice to do it OR what is the best approach for it?

1

You should declare a layout file in your custom module and then create this file in the layout directory, so in config.xml:

<?xml version="1.0"?>
<config>
    ...
    <frontend>
        ...
        <layout>
            <updates>
                <yourmodule>
                    <file>layoutfilename.xml</file>
                </yourmodule>
            </updates>
        </layout>
    </frontend>
</config>

And then create the layout file layoutfilename.xml and inside this declare your layout handles and blocks etc just like any layout file:

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        ...
        <reference name="head">
            ...
            <action method="addItem">
                <type>skin_js</type>
                <name>js/somejsfile.js</name>
            </action>
        </reference>
    </default>
</layout>
  • Say I am working off of a fresh Magento install which theme's layout directory should I add my layout file to? Say there is a custom theme installed, in that case which theme should I add my layout file to? – Paras Sood Sep 8 '14 at 17:36
  • @ParasSood always use the base/default directory for your custom theme files, because this will be used as a fallback anway. Maybe also use default/default for backwards compatibility. – Simon Sep 8 '14 at 17:57
  • Additionally, mind that it is considered best practice to include the namespace and extension name in the layout file name. Hence, use something like namespace_extension.xml. – Simon Sep 8 '14 at 18:00
  • Not sure I agree it's best practice to include the namespace in the layout filename, but certainly the module name is good to use as the filename like you see with core modules (i.e. catalog.xml, checkout.xml, review.xml and so on). Also my advice would be if your are planning to make your module available to third parties, i.e. it's not built just for that particular install then using base/default for layout and template files is best, otherwise I would say put them in the theme directory. – Jonathan Hussey Sep 9 '14 at 6:37
  • ok, great. Thank you for all the answers Simon and Jonathan. – Paras Sood Sep 9 '14 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.