Google+

Pages

Saturday, June 23, 2012

Magento: How to call static block on phtml file in magento

To call a static block on a phtml file, there are two ways:
i.    To call it with help of  [layout_file].xml
ii.   To call it directly from .phtml file

To call static block directly from .phtml file, use following syntax:

<?php echo $this->getLayout()->createBlock('[your_block_reference]')
->setBlockId('your_block_id')->toHtml() ?>

For example: If your block is a cms block, you can call it by:

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my-new-block')->toHtml() ?>

or if your block is not a cms block, and it lies in some folder(say: page/html), then you can call it by:

<?php echo $this->getLayout()->createBlock(' page/html')->setBlockId('my-new-block')->toHtml() ?>

To call it with help of  [layout_file].xml, go to:
app > design > frontend > [your package] > [your theme] > layout folder and Open the file that references the page you intend to put the block into.

Find the spot in your .xml where you would like your block to appear and insert the following code:

<block type="[your_block_reference]" name="[your_block_identifier]">
  <action method="setBlockId"><block_id> [your_block_identifier] </block_id></action>
</block>

For example:

If your block file lies in some folder(say: page/html), then you can call it by:

<block type="page/html" name="[your_block_identifier]">
  <action method="setBlockId"><block_id>page/html</block_id></action>
</block>

That's all in this section.. Enjoy.. :)


3 comments:

Kush said...

Hey thanks for the source. I was looking for this. I appreciate your work.
Magento Development India

MOHIT KUMAR ARORA said...

Thanks Kush.. :)

blogswindow said...

getLayout()->createBlock(‘cms/block’)->setBlockId(‘static_block_id’)->toHtml() ?>

http://blogswindow.com/call-a-static-block-in-phtml-magento