Question

I'm currently trying to add the sidebar cart to the header of the website I'm working on, however it doesn't seem to be working. Here are my files:

page.xml:

<block type="page/html_header" name="header" as="header">
    <block type="page/template_links" name="top.links" as="topLinks"/>
    <block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
    <block type="core/text_list" name="top.menu" as="topMenu" translate="label">
        <label>Navigation Bar</label>
        <block type="page/html_topmenu" name="catalog.topnav" template="page/html/topmenu.phtml"/>
    </block>
    <block type="page/html_wrapper" name="top.container" as="topContainer" translate="label">
        <label>Page Header</label>
        <action method="setElementClass"><value>top-container</value></action>
    </block>
    <block type="checkout/cart_sidebar" name="cart_sidebar" as="topcart" template="checkout/cart/sidebar.phtml"/>
</block>

header.phtml:

<?php echo $this->getChildHtml('topcart'); ?>

Currently, I have the cache turned off in Magento, however I have tried flushing the cache in System->Cache Management. Any ideas?

Was it helpful?

Solution

The layout name for the block you have defined (cart_sidebar) is the same as the default layout name for this block. The layout name has to be unique over the entire layout for the page, but the alias (topcart) only needs to be unique within the parent block.

Unless you have commented out other blocks with this layout name in their layout files, or better removed them from the layout using the unsetChild block method in your own local.xml (or module) layout file, then you cannot add a block with that layout name.

<action method="unsetChild"><name>cart_sidebar</name></action>

Note that using the layout remove tag does not allow you to then add a block with the same layout name, the block still exists in the layout, it just isn't output.

<remove name="cart_sidebar" />

Note also that unsetChild must be called from the parent block, but the remove tag can be applied directly under the layout handle.

Finally, page.xml should only define the basic layout structure of the page, you shouldn't add new blocks directly into this layout file, you should instead be referencing the blocks created here in other layout files and adding your own blocks there instead.

<reference name="header">
    .........
</reference>

OTHER TIPS

I'll respond to your comment in another answer as the comment doesn't give sufficient space.

The layout handle defines which pages a block will be added to, so if you look further up page.xml above the section you have posted, you see a <default> tag. This is opening layout handle tag and using default means apply the layout changes here to every page.

If you look at other layout files you will see other handles for instance in catalog.xml you find <catalog_product_view> which is the layout handle for product view pages and any layout items defined inside this handle will be only be used when viewing a product. The layout handle is created from an underscore separated concatenation of module name, controller name and action name, so with the above, the module is catalog, controller is product and action within that controller is view.

This gives you the scope you need to make layout changes just on particular pages from your own layout files. It's worth noting also that if you are not sure how to determine the layout handle for a particular page, it's inserted as the first class by default into the <body> HTML tag on each page.

Note that frontend and admin pages have separate layout files so the <default> tag only applies to either the frontend, or admin dependant on which layout file it has been placed in.

I would highly recommend you create your own local.xml layout file (or if you are creating an extension define your own layout file in it's config.xml). Using local.xml is much cleaner way to manage layouts - you leave default layout files untouched and manage all layout changes, using handles, through a single file. If local.xml doesn't exist in your themes layout directory just create it and it will be automatically picked up. Here is a local.xml layout file with some example syntax to get you started:

<?xml version="1.0"?>
<layout version="0.1.0">
    <default>
        <reference name="content">
            <block type="page/html" name="new.block" after="-" template="module/block/template.phtml" />
        </reference>
    </default>
    <catalog_product_view>
        <reference name="right">
            <action method="unsetChild">
                <name>child.block.name</name>
            </action>
        </reference>
        <remove name="some.block.name" />
        <block type="page/html" name="other.new.block" template="module/block/other_template.phtml" before="block_name_to_appear_before" parent="content" />
    </catalog_product_view>
</layout>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top