
Magento is a robust ecommerce platform that is ideal for everyone from lay users to seasoned developers. Magento offers incredible flexibility for adding, editing and setting various information items quickly and without any fuss. This way, solutions to common problems could be deployed without worrying about time constraints and application level issues.
In this article, I will demonstrate how you could easily add Magento custom layout information in module page using the Magento layout XML file. Using this file, you can add custom titles, meta tags, keywords or a description of your Magento module page. You can easily add this custom information to all existing, core or newly created modules.
What is the Magento Module Layout File?
As the name suggests, the layout file is used for rendering the Magento frontend. This Magento custom layout XML file can be found at app/design/frontend/your_vendor/your_theme/layout/YourModuleName.xml. Each Magento module has its own layout file. For example, the customer module has the customer.xml layout file and the catalog module has the catalog.xml layout file, etc.
By using the layout file, you can set or change the title, meta title, page keywords, and add breadcrumb to a module’s frontend. This can be accomplished by adding a few lines of code in the layout file of the module.
Add Magento Custom Layout Info in XML file
Open the layout XML file of a module and search for the element named as <default>. Keep in mind that you will have to write the following code inside the <default>Â element of the layout file.
<reference name="head"> Â Â Â <action method="setTitle"> Â Â Â Â Â Â Â <title>Your Title</title> Â Â Â </action> Â Â Â <action method="setDescription"> Â Â Â Â Â Â Â <title>Your Description</title> Â Â Â </action> Â Â Â <action method="setKeywords"> Â Â Â Â Â Â Â <title>Your Keywords</title> Â Â Â </action> </reference>
In addition to the above, you can add other custom information for a particular page of your module. For instance, you have a Location Page for your module. The Location Page will have a layout file named something like YourModule_index_location. You can just copy and paste the above XML code inside the layout file and change the respective information.
You can also add breadcrumbs to a specific page of your module. To add breadcrumbs, let’s take the example of the Location Page. You will have to add code so magento layout update xml works.
Open its layout file and add the following code:
<reference name="breadcrumbs"> Â Â Â <action method="addCrumb"> Â Â Â Â Â Â Â <crumbName>Home</crumbName> Â Â Â Â Â Â Â <crumbInfo> Â Â Â Â Â Â Â Â Â Â Â <label>Home</label> Â Â Â Â Â Â Â Â Â Â Â <title>Home</title> Â Â Â Â Â Â Â Â Â Â Â <link>/home</link> Â Â Â Â Â Â Â </crumbInfo> Â Â Â </action> Â Â Â <action method="addCrumb"> Â Â Â Â Â Â Â <crumbName>Location</crumbName> Â Â Â Â Â Â Â <crumbInfo> Â Â Â Â Â Â Â Â Â Â Â <label>Location</label> Â Â Â Â Â Â Â Â Â Â Â <title>Location</title> Â Â Â Â Â Â Â Â Â Â Â <link>/location</link> Â Â Â Â Â Â Â </crumbInfo> Â Â Â </action> </reference>
The above code is also useful for passing breadcrumb information to the GoogleBot, helps in SEO of your Magento website in general.
The structure of the Magento Layout XML file is easy to understand and is a great way of customizing module pages in Magento. While at first, the structure of the layout file could be tricky to understand, once you begin tinkering with it, you will quickly get the hang of the idea.
Feel free to ask a question about the usage of Layout file. Just post a comment and I will get back you ASAP!
Abdur Rahman
Abdur Rahman is the Magento whizz at Cloudways. He is growth ambitious, and aims to learn & share information about Ecommerce & Magento Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]