Magento 1 Tutorials Accessing Static Blocks In Magento

First Published: April 14, 2010

Magento 1 Static Blocks

Static blocks are a semi-useful way to allow clients to edit content without having code access. There are a lot of issues with static blocks, but sometimes they can be useful. Once you have created your static block, you can retrieve it in PHP using the code below.

// Retrieve the layout object
$layout = Mage::getSingleton('core/layout');

// Generate a CMS block object
$block = $layout->createBlock('cms/block');

// Set the block ID of the static block

// Write the static block content to screen
echo $block->toHtml();

Add the above code into any phtml file in Magento and change the 'block-id-in-magento' for the ID of the block you want to load.

You can also access a static block using the following layout XML.

<reference name="content">
  <block type="cms/block" name="">
    <action method="setBlockId"><block_id>block-id-in-magento</block_id></action>

Magento 2 Static Blocks

The following code will get a static block's HTML output in PHP in Magento 2.

// Object Manager has been used for simplicity  
$om = \Magento\Framework\App\ObjectManage::getInstance();

$staticBlock = $om->get('Magento\Cms\Block\BlockFactory')->create();

// Change the your-block-id for the correct block ID

echo $staticBlock->toHtml();

You can also get the same block via XML using the following code.

<block class="Magento\Cms\Block\Block" name="Promo">
    <argument name="block_id" xsi:type="string">your-block-id</argument>