2

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?

6
  • 1
    what value do you have for someReferenceHandle? This should be one of the existing block aliases: root, head, footer, content, left, right and so on. If you are referencing a block that does not exist, it's normal not to work.
    – Marius
    Commented Aug 7, 2013 at 11:30
  • I've been using head
    – tony09uk
    Commented Aug 7, 2013 at 12:48
  • then it should work. You should see the contents of the file ps/prefcentre/signup.phtml inside the <head> tag of your page....unless you were hoping to see it in the page. Then you need to modify the reference name to header
    – Marius
    Commented Aug 7, 2013 at 12:52
  • 2
    You should not be making more than one block an output block i.e. output="toHtml". If you are using a standard Magento layout configuration then the root block is already an output block (ref page.xml).
    – benmarks
    Commented Aug 7, 2013 at 13:04
  • would the file path change as local.xml is in frontend/base/default/layout/ and layoutFileForModule.xml is in frontend/base/default/layout/modulename/, I assume it should not matter, but as a test I added the action method=addCss node to page.xml and the css was not added
    – tony09uk
    Commented Aug 7, 2013 at 14:37

1 Answer 1

6

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>
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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