Magento Add CMS BLock to page using xml or phtml file.

While working on magento themeing, we want every block to be dynamic or editable from administrator. We can add CMS block to any page or any part of page like header and footer using following ways.

1. Using XML/Layout file

Adding cms block to any page using xml/layot file is very easy. First of all you need to define block in layout xml file like:

1
2
3
4
5
6
7
8
9
10
11
12
13
<default>
	<catalog_product_view>
		<refrence name="content">
			<block type="cms/block" name="mycustomblock" as="mycustomblock">
				<action method="setBlockId">
					<block_id>
						customBlockIdfr
					</block_id>
				</action>
			</block>
		</refrence>
	</catalog_product_view>
</default>

and After that we need to call the same block in phtml file

1
<?php echo $this->getChildHtml('mycustomblock');?>

In above example you need to replace customBlockIdfr with your cms block idenfier in backend. I am adding custom block on product detail page, you need to change according to your need.

2. Directly in phtml file

We can also add cms block to any page by writing php code to phtml file. That’s very easy and a single line code.

1
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('customBlockIdfr')->toHtml(); ?>

same as above,you need to replace customBlockIdfr with your cms block idenfier in backend.

I’ll be back with few more tricks soon in new posts.

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Skip to toolbar