Adding static blocks to specific CMS pages with Magento layouts

While working on a project, I had to add a unique static block between header and main content of the CMS page on each CMS page. Element had to be fully customizable via Magento admin panel and it had to be easy to create on new CMS pages, easily modifiable and removable. Since that element needed to be placed outside of the main content, it couldn’t have been added using CMS in Magento admin. It had to be added using Magento layouts. Here’s how I achieved that.

As you may know, Magento only offers the following handles for CMS pages:

  • Homepage handle and 404-page handle
  • A handle for all CMS pages

If a CMS block had to be shown on the homepage, we would use the homepage handle in our local.xml file. In the same way, if the same static block had to be shown on all CMS pages, we would have used a handle for all CMS pages.

But what about individual user-created CMS pages?

Frontend developer’s solution

Luckily, Magento enables us to define a custom layout for every CMS page by editing it from CMS editor on the Magento admin panel. That way, layout for a CMS page is modified using defined values for that specific page which are stored in the database.

First, we will create a static block that we want to add to a specific CMS page which we’ll namestatic-block-1 and apply some simple styles to it.

<div class=”element-blue”>
        <h3>Static Block 1</h3>
        <p>This is blue static element 1 </p>
</div>

Next, we will create a new CMS page that will contain the created static block which will be added through Layout Update XML on Designs tab, along with a header and one paragraph. For this example, we’ll use 1 column layout for all CMS pages.

<reference name="root">
        <block type="cms/block" name="myelement">
            <action method="setBlockId"><block_id>static-block-1</block_id></action>
        </block>
</reference>

And finally, we have to update our template files and add a line of code which calls our element.Since we used column layout for all CMS pages and we will modify 1column.phtml template file. If you want to add the elements to CMS pages that use some other template (like 2columns-left), you need to modify those files. 

<div class="page">
        <?php echo $this->getChildHtml('header') ?>
        <?php echo $this->getChildHtml('myelement') ?>
…

For this example, I’ve made one additional static block that is different than the first one and two additional CMS pages – one for the new element I’ve created and another one that won’t have any layout updates and because of that, It won’t contain any of the elements that we’ve created.

Results

CMS page 1 with blue static block:

CMS page 1

HTML code of CMS page 1:

01-code-1

CMS page 2 with green static block:

02-cms-2

HTML code of CMS page 2:

02-code-2

CMS page 3 for which we didn’t set any layout updates in its XML and therefore it doesn’t show any created static blocks.

03-cms-3

HTML code of the CMS page 3:

03-code-3

The good and the bad and the alternative

Although this is a really quick and simple solution that works well, it becomes tedious when you have to go through these same steps for every element that you want to add, even on a project that had 6 or 7 CMS pages.

If you want a more backend-oriented solution for this, Tim Reynolds, Magento Certified Developer, wrote an article on a topic of adding widgets to specific CMS pages in which he used event observers to generate unique handles for each CMS page.

So, how do you like this approach? Feel free to share your solutions in the comment section.


source: http://inchoo.net/magento/adding-static-blocks-to-specific-cms-pages-with-magento-layouts/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值