Displaying Magento Static Block Content in PHTML Templates
Hello, developers! Today we’re going to tackle an issue that many of us have faced while working with Magento: calling a CMS static block from a PHTML template. Don’t worry; by the end of this post, you’ll be able to display any custom content you create in the admin panel right where you need it.
The Challenge: Calling a Static Block
Let’s say you’ve created a new static block in your Magento admin under Content > Elements > Blocks (ID: sample_links). You want to display the content of this block in your footer.phtml template. Seems simple enough, but sometimes getting Magento to play nice can be a challenge!
The Approach: Using Layout XML and PHTML
You’ve already started on the right track by editing your page.xml layout file. Here’s what you did:
<reference name="foot_lnk">
<block type="cms/block" name="sample_block" before="-">
<action method="setBlockId"><block_id>sample_links</block_id></action>
</block>
</reference>
Great! This tells Magento to insert a CMS block (with ID ‘sample_block’) into the foot_lnk block in your footer layout.
The Problem: Why isn’t it Working?
So why aren’t you seeing your static block content when you call $this->getChildHtml('foot_lnk') in your footer.phtml file?
The issue lies in how Magento handles the insertion of blocks. When you use <block type="cms/block">, Magento expects to find a template for that block.
By default, it looks for a cms/block.phtml file, which doesn’t exist in standard Magento installations. As a result, your static block content isn’t being rendered correctly.
The Solution: Specifying the Template
To fix this, you need to tell Magento where to find the template file for your CMS block. You can do this by adding an attribute to your block node:
<reference name="foot_lnk">
<block type="cms/block" name="sample_block" before="-">
<action method="setBlockId"><block_id>sample_links</block_id></action>
<attribute name="template">cms/block.phtml</attribute>
</block>
</reference>
Now, when Magento tries to render your CMS block, it will use the cms/block.phtml template. This file does exist in standard Magento installations.
The Bonus: Creating a Custom Template
If you want even more control over how your static blocks are displayed, consider creating a custom PHTML template for them.
-
Create a folder named
blocksunder your theme directory (e.g.,app/design/frontend/{YourPackage}/{YourTheme}/template/cms). -
Inside the
blocksfolder, create a new file named after your block (e.g.,sample_links.phtml). -
Add your custom HTML and PHP logic to this new template.
-
Then, modify your layout XML file to reference the new template:
<reference name="foot_lnk">
<block type="cms/block" name="sample_block" before="-">
<action method="setBlockId"><block_id>sample_links</block_id></action>
<attribute name="template">cms/sample_links.phtml</attribute>
</block>
</reference>
Now, your static block will use the custom template you’ve created!
Conclusion
Calling Magento blocks from PHTML templates can be a bit tricky at first. But with the right approachâediting layout XML files and specifying the correct template pathsâyou’ll be able to display any CMS block content wherever you need it in your site’s design.
Remember, if you’re looking for more advanced tips on working with Magento themes and templates, check out our guide How to Create Custom Magento Themes.
Happy coding!