40

I have a static block called newest_product (with content) and I would like to display it on a .phtml file as html.

I've tried this code:

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

But this nothing is being displayed.

Am I using the wrong code?

Tim Penner
  • 3,551
  • 21
  • 36
iamjonesy
  • 24,732
  • 40
  • 139
  • 206

8 Answers8

78

If you have created CMS block named 'block_identifier' from admin panel. Then following will be code to call them in .phtml

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml(); 
?> 
Suman-PHP4U
  • 1,185
  • 11
  • 13
  • 3
    I like this approach more than the one @Mcs mentioned. Using layout is most of the times messy – karantan Mar 02 '15 at 13:06
  • Like it also. And more simple than the __Mage::getModel('cms/block')__ method, which also works – Sunry Nov 25 '15 at 03:23
  • @karantan setting ID in phtml file is basically hard-codding it in the template. Layouts represents configuration part of a system where id is expected to be used. Hope it helps. – Max Pronko Jan 05 '17 at 16:01
50

In the layout (app/design/frontend/your_theme/layout/default.xml):

<default>
    <cms_page> <!-- need to be redefined for your needs -->
        <reference name="content">
            <block type="cms/block" name="cms_newest_product" as="cms_newest_product">
                <action method="setBlockId"><block_id>newest_product</block_id></action>
            </block>
        </reference>
    </cms_page>
</default>

In your phtml template:

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

Don't forget about cache cleaning.

I think it help.

Max Pronko
  • 1,369
  • 11
  • 13
  • Any workaround to get title of Child CMS block? I can fetch title through php but I have no idea how to get title of block that is loaded by layout xml.. TIA – Kamal Joshi Jul 31 '13 at 15:55
  • Don't forget it's typically better to use a local.xml in your layout theme instead of overriding individual xml files. – bassplayer7 Nov 13 '13 at 16:58
  • "What is redefined to to suit my needs???" - it means that you need to change this handle to the one that you are using. http://devdocs.magento.com/guides/m1x/magefordev/mage-for-dev-4.html – Tisch Sep 15 '15 at 16:02
  • I believe you can just use the value after 'as' for a parameter of getChildHtml(). Adding the ""-line isn't necessary. – Daan van den Bergh Nov 24 '16 at 14:54
21
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_name')->toHtml() ?>

and use this link for more http://www.justwebdevelopment.com/blog/how-to-call-static-block-in-magento/

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Ayush Sugandhi
  • 339
  • 2
  • 3
14

If you want to load a cmsblock into your template/blockfile/model etc. You can do this as followed. This will render any variables places in the cmsblock

$block  = Mage::getModel('cms/block')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->load('identifier');

$var = array('variable' => 'value', 'other_variable' => 'other value');
/* This will be {{var variable}} and {{var other_variable}} in your CMS block */

$filterModel = Mage::getModel('cms/template_filter');
$filterModel->setVariables($var);

echo $filterModel->filter($block->getContent());
Jeroen
  • 159
  • 1
  • 4
  • This is the only way to get your block to display from a .phtml file including variables in the cms block without the use of XML and `getChildHtml()`. Thank you. – MrUpsidown Aug 06 '14 at 14:45
8

I think this will work for you

$block = Mage::getModel('cms/block')->setStoreId(Mage::app()->getStore()->getId())->load('newest_product');
echo $block->getTitle();
echo $block->getContent();

It does work but now the variables in CMS block are not parsing anymore :(

Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
Kanak Vaghela
  • 7,750
  • 10
  • 32
  • 37
5

Following code will work when you Call CMS-Static Block in Magento.

<?php echo 

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

?>
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
user3057379
  • 59
  • 1
  • 3
2

This should work as tested.

<?php
$filter = new Mage_Widget_Model_Template_Filter();
$_widget = $filter->filter('{{widget type="cms/widget_page_link" template="cms/widget/link/link_block.phtml" page_id="2"}}');
echo $_widget;
?>
2

When you create a new CMS block named block_identifier from the admin panel you can use the following code to call it from your .phtml file:

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

Then clear the cache and reload your browser.

Joshua Kleveter
  • 1,769
  • 17
  • 23