Blocks in Magento

One of the things Magento got right is blocks. It’s one of the few things I am continually impressed by. There are a lot of ways to add a static block to the page.

XML is probably the most common place to add a static block. You find whatever block you want to add your static block into and you add something like:

<block type="cms/block" name="YOUR_BLOCK_ID" before="-">
	<action method="setBlockId"><block_id>YOUR_BLOCK_ID</block_id></action>

Adding a static clock through layout XML can be very useful if you need to add a static block to a sidebar on a single page for example instead of all pages.

<reference name="left">
	<block type="cms/block" name="YOUR_BLOCK_ID">
		<action method="setBlockId"><block_id>YOUR_BLOCK_ID</block_id></action>

Adding you static block through PHP may be the least common way. This, however, can be very useful if you want to define areas in your theme where a menu or a widgets could go. To add a static block in PHP you would use:

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

Adding a static block through a short code is very powerful. It allows you to add a static block into a CMS page or even your products.

{{block type="cms/block" block_id="YOUR_BLOCK_ID"}}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>