Magento – Fix Duplicate Block Output in XML Layout

blockstemplate

i'm creating a menu in this way:

<block type="page/template_links" name="footer_links2" as="footer_links2" template="page/template/links.phtml">
    <action method="setTitle"><title>Main menu</title></action>
    <action method="addLink" translate="label title" module="catalogsearch">
        <label>About us</label>
        <url helper="cms/page/getPageUrl"><pageId>3</pageId></url>
        <title>About us</title>
    </action>
    <action method="addLink" translate="label title" module="catalogsearch">
        <label>Contact us</label>
        <url helper="cms/page/getPageUrl"><pageId>4</pageId></url>
        <title>Contact us</title>
    </action>
    <!-- Other links here... -->
</block>

and it works, but i want that the same menu appears in a different part of the page too with the same content. Is there a way to duplicate the output of that block so that i can show it without duplicating the code?

Best Answer

Through layout XML, you will have to repeat the block calling for each reference. like if you want to call the same block in header, you will have to do following:

<reference name="header">
 <block type="page/template_links" name="footer_links2" as="footer_links2" template="page/template/links.phtml">
    <action method="setTitle"><title>Main menu</title></action>
    <action method="addLink" translate="label title" module="catalogsearch">
        <label>About us</label>
        <url helper="cms/page/getPageUrl"><pageId>3</pageId></url>
        <title>About us</title>
    </action>
    <action method="addLink" translate="label title" module="catalogsearch">
        <label>Contact us</label>
        <url helper="cms/page/getPageUrl"><pageId>4</pageId></url>
        <title>Contact us</title>
    </action>
    <!-- Other links here... -->
  </block>
</reference>

and then call this block by $this->getChildHtml('footer_links2'); method. you will have to repeat it for any other reference.
but if you don't want to repeat these steps, you can create a static block, enter the links in it and then call that static block anywhere you want it to. So one change will update the links wherever this static block is shown.

Related Topic