Magento – using local.xml to avoid repetition

layout

I hope you don't mind me expanding on this question. Using what's explained I got the css working in my template file. But I have not been able to get it working from local.xml, so I am repeating myself several times within the same layout file.

I got to thinking that a local.xml file would be useful in this situation, so what I tried to do was take the node that worked in the layout file and wrap it in a reference node in local.xml and call that reference node

This works

frontend/base/default/layout/modulename/layoutFileForModule

<user_register_form>
    <block type="page/html_head" name="head" output="toHtml">
        <action method="addCss"><stylesheet>css/normalize.css</stylesheet></action>
    </block>
    <block type="core/template" name="prefcentre_regform" output="toHtml" template="ps/prefcentre/signup.phtml" />
</user_register_form>

This does not work

frontend/base/default/layout/local.xml

<reference name="someReferenceHandle">
    <block type="page/html_head" name="head" output="toHtml">
        <action method="addCss"><stylesheet>css/normalize.css</stylesheet></action>
    </block>
</reference>

frontend/base/default/layout/modulename/layoutFileForModule

<user_register_form>
    <reference name="someReferenceHandle">
        <block type="core/template" name="prefcentre_regform" output="toHtml" template="ps/prefcentre/signup.phtml" />
    </reference>
</user_register_form>

I have misunderstood a tutorial somewhere, I thought Magento would look for the layout.xml file first when it compiles all the .xml files, then by me using the reference handle I was able to reference the block within that handle.

Why does example one work and example two fail?

Best Answer

Yes, you seem to have misunderstood the syntax involved in what you want to do (though it seems you've understood the principles, and that's half the battle!).

  1. Don't use an output block with a typical Magento layout, as the root block is already set as an (the) output block (ref. page.xml):

    <block type="page/html_head" name="head" output="toHtml"> <!-- NO output -->
    
  2. You should not be redeclaring the head block; rather, you should reference the one already created (again, ref page.xml):

    <!-- <block type="page/html_head" name="head" output="toHtml"> This replaces the original! -->
    <reference name="head">
        <action method="addCss"><stylesheet>css/normalize.css</stylesheet></action>
    </reference>
    
  3. Collect would-be-repeated directives in a custom layout update handle and use the <update> directive to include them in other handles:

    <UTILITY_HANDLE>
        <reference name="head">
            <action method="addCss"><stylesheet>css/normalize.css</stylesheet></action>
        </reference>
        <reference name="content">
            <block type="core/template" name="prefcentre_regform" template="ps/prefcentre/signup.phtml" />
        </reference>
    </UTILITY_HANDLE>
    <!-- include the above on catalog product view page -->
    <catalog_product_view>
        <update handle="UTILITY_HANDLE" />
    </catalog_product_view>
    
Related Topic