Magento CMS static blocks
14 Jul 2009, by Peter Spiller
5 Comments · Posted in Magento

    Static blocks in Magento are a useful way of making elements of your site easily configurable by administrators. If you have sidebar blocks, headers or other elements that can change from time to time, using a static block can be easier in the long run than hard-coding them.

    Static blocks are very simple to create. In the admin backend, just go to the CMS menu and select Static Blocks. Click on the Add New Block button to create a new block. The fields for a static block are also very simple - you can give the block a title (used to distinguish blocks in the backend) and you can select whether it is enabled or not. The Content field is where you can enter your block HTML. You can use Magento template placeholders in this field (i.e. https://static.fontis.com.au/skin/frontend/base/default/...).

    The Identifier field assigns the block a name that Magento will use to refer to it. You can also select what store views the block will appear in. You can combine this with the Store Views field to create different versions of a block for different stores. For example, say you have two different store views for two different countries (e.g. Australia and the US), and you have a block containing contact details that vary from country to country (such as phone numbers). If you create a block with the identifier 'contact' and set its store view to the Australian store view, then create a second block that also has the identifier 'contact' but set its store view to the US store, the appropriate block will be used on each store.

    Once you have created a static block, you need to tell Magento to actually put it somewhere. Certain other Magento configuration sections can use static blocks (such as the catalog category properties, as seen in the Magento static blocks screencast), but you can also include them directly in a number of other ways.

    If you want to include a static block inside another static block or CMS page, you can use a Magento template placeholder (replace 'IDENTIFIER' with the static block identifier):

    
    ...
    This is some CMS page text. A static block goes below this:
    {{block type="cms/block" block_id="IDENTIFIER"}}
    More text after the block...
    ...
    

    You can also include a static block from a layout XML file (replace 'BLOCK_NAME' and 'IDENTIFIER' with appropriate values - the block name just needs to be unique). If you open any Magento layout files, you can see where the blocks are defined; you can add your own blocks after the existing ones. The 'before' and 'after' attributes indicates where your static block appears relative to other blocks in the same layout section.

    ...
      <block type="cms/block" name="BLOCK_NAME" after="cart_sidebar">
        <action method="setBlockId"><block_id>IDENTIFIER</block_id></action>
      </block>
    ...
    

    Finally, you can include a static block directly from a template PHP file (again, replace 'IDENTIFIER'):

    ...
    Template text...
    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId(IDENTIFIER)->toHtml() ?>
    More template text under block...
    ...
    

    Comments

    Thank you! This clears things up tremendously for me! I was always confused in thinking that I had to update both the layout and template files in order to add the blocks. I really appreciate this post.
    Comment by Anthony - 22 Jul 2009 11:43:58 PM
    I found that using the PHP method, it would throw up a massive error if i didn't include IDENTIFIER in single quotes, ie. ..->setBlockId('IDENTIFIER')->..

    Cheers for the guide though, makes it way easier to understand.
    Comment by Moe - 31 Jul 2009 10:40:30 AM
    How do you reference it so they only appear on the homepage
    Comment by simon - 10 Feb 2010 4:03:36 PM
    Thank you very much, very helpful!
    Comment by Jeremy - 28 May 2010 6:09:26 PM
    Thank you! This clears things up tremendously for me! I was always confused in thinking that I had to update both the layout and template files in order to add the blocks. I really appreciate this post.
    Comment by maxi dress - 29 Sep 2010 4:35:00 PM
    Comments are closed for this post