Magento: Adding a static block to a view file (AKA: phtml file)
In magetnto you can add static blocks that you have created to pages using the layout xml files and adding code there. There is plenty of documentation on this method, but what if you want to call the static block from a view (view files generally end in .phtml). Well this is how I have been able to do it.
Just call
<?php
echo $this->getLayout()->createBlock('cms/block')->setBlockId('contacts_text')->toHtml() ;
?>
or
<?php
echo $this->getLayout()->createBlock('catalog/product_list_related')->setTemplate('catalog/product/list/related.phtml')->toHtml() ;
?>
Where setBlockId is the identifier you set up in the static block.

15 Responses to “ Magento: Adding a static block to a view file (AKA: phtml file) ”
May 22nd, 2009 at 6:05 am
I use Free CMS module, it’s very useful. You can see the demo here: http://demo.hello-magento.com/freecms/furniture.html
June 17th, 2009 at 12:23 pm
I’m trying to do the later example with setTemplate() however I’m using a custom theme and it keeps attempting to load the template files form the default theme instead of my current theme. Is there a way to specify my theme instead of using the default?
June 17th, 2009 at 2:43 pm
That sounds like maybe the block doesn’t exist in your custom files so it is defaulting to default. What Block are you trying to call. Is it overriding a core block? You may need to add your custom name. Ex: if you are storing your custom code in app/code/local/Sharpdot/catalog/
Try createBlock(‘Sharpdot_catalog/product_list_related’)
Where Sharpdot is what you are using
September 21st, 2009 at 6:28 am
very awesome tutorial you save my life
October 13th, 2009 at 12:20 am
Hi,
Thanks for your help.
Just 1 question…
how can i pass parameters to this block.
Suppose from cms section (In Admin), when i make reference to block, i can pass email id..for example
and in form.phtml file i can get this using
<input type=”hidden” id=”recipient” name=”recipient” value=”getData(“recipient”);?>” />
I want to pass recipient email id to block from phtml file, means from your explaining method.
So how can i do this?
Please help if you can.
Thanks for your help.
October 13th, 2009 at 6:15 am
@smivk here is a link that may help you: http://www.magentocommerce.com/boards/viewthread/34102/.
I believe you can pass paramiters like this
$this->getLayout()
->createBlock($this->_successBlockType)
->setOrder($this->_order)
->setMyVariable($request) //Simply add this?!!
I have not tried this though.
October 14th, 2009 at 8:21 pm
Hi mdost,
Thanks for your reply…
I solve it my self by using
$this->getLayout()->createBlock(‘core/template’)
->setData(array(‘recipient’=>$thisObj->getEmail()))
->setTemplate(‘contacts/form.phtml’)->toHtml() ;
Thanks for your help again..
November 12th, 2009 at 8:16 am
Just another point. In case you are using this code in a module it may not work as $this does not represent the view. You can then use:
Mage::app()
->getLayout()
->createBlock(’cms/block’)
->setBlockId(’block_identifier_here’)
->toHtml();
Cheers
January 22nd, 2010 at 12:36 pm
Thanks for this! I’ve found a number of things on your website useful.
April 5th, 2010 at 7:07 am
Awesome method! I find the Magento API a real struggle to come to terms with.
I was using page.xml to set up things like
en_primeur
Then call $this->getChildHtml(‘en_primeur’); in a .phtml file. This has been working great for left or right.phtml. But ran into issues when trying to do this in template/catalog/product/view.phtml or any of the product view files. I needed to check if a wine bottle was “en_primeur” or not (en primeur means gst exclusive and arrives in a few months) and display a custom note that may change.
Your method of making a static block call at run time with PHP solved it. Thanks!
May 19th, 2010 at 7:07 am
Any one know how to add a static block to Customer Dashboard?
May 19th, 2010 at 8:53 am
Here is a quick and broad answer:
in “/app/design/frontend/yourtemplatepath/yourtemplatepath/template/customer/account” is where the template files are stored for the customer account area. I guess it just depends where you want to add the block as-to which file you need to edit.
NOTE:This is allittle of of this posts topic, but you can also inject blocks using the layout xml files. The customer.xml layout file will also allow you to add blocks. Here again it just depends on what and where you want to add the block.
Hope that helped.
May 19th, 2010 at 9:03 am
Yes but in the version 1.4.0.1 that folder only have navigation.phtml file.
May 19th, 2010 at 9:11 am
In Magento 1.4.x They implemented a new way of theming.
The base most files are located in /app/design/frontend/base/.
If the theme doesn’t have to modify the file then it is only in the base folder. Just copy any files you need from the base folder to your themes folder. Be sure to maintain the folder structure in your themes directory.
May 19th, 2010 at 9:44 am
Great, that is the info that I need. Thank you very much*