Adding a New Block to Your Layout in Magento 1.9: A Step-by-Step Guide
As an experienced Magento developer, I often encounter questions from beginners like you who are struggling with layouts. You’re on the right track thinking about adding your banner block below the header and outside of it. Let’s dive in and clarify this process.
Understanding the Basics
In Magento layout updates, you’ll work with XML files located under app/design/frontend/<package>/<theme>/layout. These files define how blocks are arranged on a page.
The cms.xml file is used to define CMS-specific layouts. If your banner needs to be displayed on various pages but not just CMS ones, we won’t modify this file directly.
Creating Your New Layout File
Let’s say you want to add the new block called my_banner to your layout and name it homepage.phtml. Here are the steps:
-
Create a new XML file: Navigate to
app/design/frontend/<package>/<theme>/layoutand create a new file namedyour_layout_file.xml. -
Add reference handles: Inside this new file, you need to add references to existing handles where you want your block to appear.
Here’s an example of how to add your banner below the header:
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.9.0">
<default>
<reference name="header">
<block type="cms/block" name="my_banner">
<action method="setBlockId"><block_id>homepage</block_id></action>
</block>
</reference>
</default>
</layout>
In this code, header is the reference handle for the header block. The <block> element creates a new CMS block named my_banner, and homepage should be your PHTML file location under cms/block.
Adding Content to Your New Block
Now let’s create the actual PHTML content:
-
Locate or create your .phtml file: Navigate to
app/design/frontend/<package>/<theme>/template/cms/block/and either locate or create a new file namedhomepage.phtml. -
Add your HTML content: Inside this file, add the HTML you’d like to display for your banner.
For example:
<div class="my-banner">
<img src="{{skin url='images/banner.jpg'}}" alt="Banner Image" />
</div>
This will display an image using Magento’s skin helper.
Conclusion
Adding new blocks to your layout is a fundamental aspect of customizing your Magento store. By following this guide, you’ve learned how to:
- Create a new XML file for your layout
- Reference existing handles (like header) in the layout
- Create a new CMS block and set its content
Remember, every theme has its structure, so always check your specific theme’s documentation or contact our experts at Laravel Company for further assistance.
The key is to understand where you want to place your custom content (in this case, below the header) and which existing handles you can reference. With practice, you’ll become proficient at managing Magento layouts.