Magento CMS : 2 methods for static blocks

Magento's backoffice comes with a fairly simple CMS that can do a lot : single pages can be modified (content and layout) via the CMS / Manage Pages menu, and page parts can be shared and modified via the CMS / Static Block menu. Web developers who get a first sight inside templates might find it difficult to integrate a newly created block : we describe here two methods to integrate a block in your magento website.

How to create a CMS block : Go to Magento admin uder CMS -> Static Blocks and Add New Block. Insert Block Title, Identifier (for example: mycompany-newsblock), set Status to Enabled, insert block Content and Save Block.

How to add the CMS block to pages (exemple on the right column). You can choose one of these 2 methods

  1. LAYOUT block  modification: in layout/page.xml , find the default / right column block
    <block type="core/text_list" name="right" as="right" />

    and transform it by incorporating the newly created CMS block  :
    <block type="core/text_list" name="right" as="right">
    <block type="cms/block" name="ruedesiam.marge" as="ruedesiam.marge" after="-">
    <action method="setBlockId"><block_id>mycompany-newsblock</block_id></action>
  2. Template block addition : add the following code to

    <?= $this->getLayout()->createBlock('cms/block')->setBlockId('mycompany-newsblock')->toHtml() ?>

The solutionbelow explains how to add a template block : everything here is done via HTML files. (example for theleft column)

1. in layout/page.xml or in your page custom design, add the following code

<reference name="left">
<block type="core/template" name="productbrand" alias="productbrand" as="productbrand" template="catalog/product/brand.phtml"  />

2. in template/callouts/ left_col.phtml, add the following :

<? $this->getChildHtml('productbrand') ?>

Note : you can avoid xml configuration by creating the core template directly within the phtml :

 <?php  echo $this->getLayout()->createBlock('core/template')->setTemplate('callouts/rassure.phtml')->toHtml() ?>

<?php  echo $this->getLayout()->createBlock('core/template')->setTemplate('callouts/rassure.phtml')->toHtml() ?>

4 replies
  1. Jeff
    Jeff says:

    Anyone who's thinking about using magento should have their head examined; magento is bloated, confusing and bullocks. I'd only suggest using it if you've got a penchant for punishment, or 6 months to learn all their "simple" coding conventions ... But don't expect to do this by reading their documentation, or searching for help on their forums; the information you'll find that way is lacking to say the least. You'll only learn it by wading through the 5,000 files it installs on your server; you read that write, the standard install contains nearly 5,000 files. I stamp magento with an AVOID AT ALL COSTS label. Future users beware!

  2. Tim
    Tim says:

    ...not sure if this is in the right area or you can help but I am having trouble adding a static block in a newsletter template. I have tried adding {{block type="cms/block" block_id="block-id"}} in the template but on preview it interprets this as a string and the static block does not appear. No response from the Forum. Any ideas? Thanks.
    ( - mobile computing office bundles)


Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *