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
getLayout()->createBlock('cms/block')->setBlockId('contacts_text')->toHtml() ;
?>
or
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.

24 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*
October 23rd, 2010 at 10:33 am
Good approach, thanks.
But it’s recommended to create separate block method for this purposes.
October 23rd, 2010 at 3:30 pm
In magento there are many ways to do this. It really depends on what you are developing.
In the first example it was calling a cms block created in the admin so in that case you probably will not want to create a new method.
In the second example the block that was created was extending “core/template” so in that case the “toHtml()” is the method used to render the html so there is no need to add a new method either.
November 9th, 2010 at 10:02 pm
Thanks Dude. It really works ………….
..
January 11th, 2011 at 5:59 am
You save my time one more time.
Thanks.
June 13th, 2011 at 3:47 pm
Wish this link would have been at the top of Google because it would have saved me half a day.
November 7th, 2011 at 4:21 am
I found very good example on how to include static blocks.
http://www.phptechi.com/how-to-add-static-block-in-magento.html
December 1st, 2011 at 1:51 pm
Hi, and thanks for this tutorial. I am trying to add a static block only on my searchresult page.
I wonder which phtml fil and where should I add this php code to?
best regards
Emil
December 1st, 2011 at 2:00 pm
@emil,
To see which template files control different parts of the page just enable “Template Path Hints”.
1. Go To System-> Configuration -> Advanced -> developer -> debug
2. Change the “Current Configuration Scope” to the website.(this is the dropdown list in the top left of the page)
3. Now The “Debug” fieldset set “template path hints” and “add block names to hints” to Yes.
Now when you refresh the frontend you will see the block class and template paths for the different parts of the page. When you found what you wanted set “template Path Hints” back to “No”.
the results files are these two
/app/design/frontend/Yourtheme/YourTheme/template/catalogsearch/result.phtml
AND
/app/design/frontend/Yourtheme/YourTheme/template/catalogsearch/advanced/result.phtml
December 2nd, 2011 at 1:41 am
Hi, I got it. Thanks for such a detailed help