How to display Magento CMS blocks in template files programmatically

There are a lot of reasons you might want to call cms blocks programmatically in magento inside your template or inside your own plugin. Particularly cms blocks are very useful to allow admin access to html from control panel avoiding the need to change theme files.  So, how do you display a cms block inside a theme phtml file?

Here is how to get the CMS helper:


  1.  $cmsHelper = Mage::helper('cms');

Now to get the CMS block by id, you ll need to do this


  1.  $block = Mage::getModel('cms/block')->load("your CMS block id here");

It is better to check if the CMS block is active by:


  1.  if ($block->getIsActive()){

To get CMS block title:


  1.  $block->getTitle()

To get CMS block content/description:


  1.  $block->getContent()

However when you echo description directly you might notice it contains unprocessed magento path variables, therefore you’ll need to convert those to what they mean using Magento’s own method¬†getPageTemplateProcessor


  1.  $processor = $cmsHelper->getPageTemplateProcessor();
  2.  $block_content=$processor->filter($block->getContent());
  3.  

Here is a complete example that get the content and title of a CMS block, checking if it is active and process its variables


  1.  $cmsHelper = Mage::helper('cms');
  2.  $processor = $cmsHelper->getPageTemplateProcessor();
  3.  $block = Mage::getModel('cms/block')->load("my_cms_block_id");
  4.  if ($block->getIsActive()){
  5.   echo "Title:".$block->getTitle()."<br/>";
  6.   echo "Content:".$processor->filter($block->getContent())."<br/>";
  7.  }
  8.  

Leave a Reply

Your email address will not be published.