Duplicate Magento Block Output in XML Layout: A Comprehensive Guide
As a Magento developer, you often find situations where you need to reuse blocks across different parts of your storefront without duplicating code. This not only saves time but also ensures consistency in your menu structure. In this guide, we’ll explore how to duplicate the output of a block in Magento using XML layout files.
The Challenge: Duplicating Menu Blocks
You’ve created a menu block that appears in the footer as shown:
<block type="page/template_links" name="footer_links2" as="footer_links2" template="page/template/links.phtml">
<action method="setTitle"><title>Main menu</title></action>
<!-- ... Other links here... -->
</block>
This block works perfectly and displays the “Main menu” in the footer. Now, you want to display this exact same menu in another part of the page, like the sidebar or header.
The Solution: Reusing Blocks via XML Layout
Instead of duplicating the entire footer_links2 block and its actions, we can reuse it using Magento’s XML layout system. Here’s how:
-
Create a new layout file: Navigate to your theme directory (
app/design/frontend/{package}/{theme}) and locate or create a new XML layout file under thelayoutfolder.Let’s say you want to add this menu to the header. You can create a file named
catalog.xml:<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/layout.xsd"> <!-- Reusing footer_links2 block --> <referenceContainer name="page.top.links"> <block type="Magento\Theme\Block\Html\PageTopLinks" template="Magento_Theme::html/page.toplinks.phtml"> <arguments> <argument name="cache_lifetime" xsi:type="number">86400</argument> </arguments> <!-- Reuse footer_links2 block --> <block type="page/template_links" name="header_links" as="header_links" template="page/template/links.phtml"> <action method="setTitle"><title>Header menu</title></action> <!-- Other links and actions here... --> </block> </block> </referenceContainer> </config> -
Reference the new layout file: To apply this layout to a specific page or section, you need to reference it in your XML layout files using the
<referenceLayout>tag. For example:<referenceLayout name="catalog_category_view"> <!-- Include our new catalog.xml --> <include module="Your_Module" file="catalog.xml"/> </referenceLayout>
Replace Your_Module with your actual module name.
- Refresh layout: After making these changes, you need to refresh Magento’s layout by deleting the contents of the
var/cache/directory and clearing your browser cache.
Conclusion: Reusing Blocks for Cleaner Code
By following this approach, we successfully duplicated the output of our “Main menu” block without duplicating any code. The block is now displayed in both the footer and the header using a single set of XML actions.
This technique promotes cleaner, more maintainable code by avoiding duplication. It also ensures consistency across your siteâif you update one instance of the “Main menu”, all reuses will automatically reflect those changes.
Remember to always refresh Magento’s layout cache after making these changes for them to take effect.
For more advanced Magento development techniques and best practices, feel free to explore our Magento Development Resources at Laravel Company.