There are two ways you can output static CMS blocks in Magento’s “.phtml” files.

Method 1. Using XML (Recommended)

Insert the following code inside the view block that you want it to be accessible in. In the example below I have inserted it into the “product.info” block in the “” layout handle. This can be found at : app/design/frontend/your_theme/default/layout/catalog.xml. If it is not there then you will need to copy it over from the base folder, this can be found at app/design/frontend/base/default/layout/catalog.xml.

1
2
3
4
5
6
7
8
9
10
11
<catalog_product_view>
    <reference name="content">
        <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">
            <block type="cms/block" name="my_block">
                <action method="setBlockId">
                    <block_id>block_id</block>
                </action>
            </block>
        </block>
    </reference>
</catalog_product_view>

Then in your view script (catalog/product/view.phtml), the block will be accessible using :

1
echo $this->getChildHtml('my_block');

Method 2. Using View Script

With this method you will not need to edit any XML layout configuration as you are creating the block on the fly and then outputting it manually using the “toHtml” method of the “Mage_Core_Block_Abstract” class. This method gets called on all Magento view blocks that produce any kind of output.

1
echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_block')->toHtml();

Conclusion

As you can see both these methods do virtually the same thing, the reason I prefer using method one is because it allows Magento to cache the layout configuration, improving page load speeds etc.

Gavin Rogers

Gavin has consulted with many brands of all sizes over the years. He collaborates with clients on detailed, tech-heavy Magento solutions and also recently built and sold a Twitter tool, "Twenth" - used by celebrities worldwide.