Home » Magento Customization, Magento Guide

How to Add Dynamic Content to Magento Static Blocks and Pages?

29 July 2009 3 Comments

Magento provides an easy way to create blocks of static content which, with a little Layout-XML-Fu, can be added to any page in Magento. Even better, these blocks can be added to any category page with no programming at all. This is great if you want to have a banner image with monthly specials and deals, or if you want to add a header image or descriptive text to a category.

Magento also makes it really easy to create static SEO landing pages. In just a few clicks you have a new static page, ready to lace with images and tasty keyword laden text, and to link to any product or category in the Store proper.

But you can’t put PHP code in these blocks or pages.

Not directly at least…

Magento Static Blocks and Pages will process some predefined template tags. For instance, to make a link to your Magento store’s front page you can use the “store” template tag:

<a href="{{store url=""}}">Home</a>

Similarly, you can use the “block” tag to load any dynamic theme template into a static block! This means you can put dynamic content in static blocks and pages. You can use existing Magento template “blocks”, or create your own custom ones. Here is an example of inserting the Newsletter Subscribe form template into a static block or page:

{{block type='core/template' name='contactForm' template='newsletter/subscribe.phtml'}}

You can also insert dynamic template blocks from custom modules you wrote. You can basically write any PHP your heart desires in a custom module, and load it into a static block or page as easy as this:

{{block type="mycompany_subcatthumbs/thumbview" template="subcatthumbs/thumbview.phtml"}}

The syntax for these “block” tags is basically the same as in the Layout XML files, with type, template and other parameters:

<block type="core/template" name="right.permanent.callout" template="callouts/right_col.phtml"/>

Why would you want to this though? They are called “static blocks” after all, so why should they ever need to be dynamic? One example of a situation where I use this trick is to display a category’s sub-categories as thumbnails under the main category. I wrote a small custom module for this (thanks Jake Rutter ), which has a template block called “thumbview.phtml”. Most of the time, it’s true, it’s better practice to add this block via Layout XML, either directly in the file or though the Layout Update XML under the Custom Design tab. But if I added that to the catalog.xml layout file then it would appear on all category pages (not just the ones with sub-categories) and I would have to add logic to fix this, etc. I had to add Static Blocks with header images to the categories already, so it was easier to just paste the {{block}} tag in at the same time. This way I didn’t have to alter the core Catalog/List logic and I don’t have to keep track of Layout Update XML changes: if the client wants to just show products instead of subcategories they can just turn off the Static Block, removing the header image and the subcategory list in one fell swoop.

I’m 99% sure there is always a way to achieve the effect you want in Magento without using this trick/hack, but sometimes it’s just easier and faster.

We have also developed a powerful extension that allow you to create dynamic block. The block support full HTML content and can place anywhere on your site. This very strong and flexible in advertisements and promotion. See more information here.

Related posts:

  1. Tips For Creating Dynamic Category Landing Pages
  2. Simple random banner rotator in Magento using static blocks
  3. Some custom Blocks to help you show products
  4. Programatically create Magento blocks and inject them into layout
  5. Add New Reference in Magento

3 Comments »

  • Julien said:

    Oh thanks for this explanation !

  • Darshana said:

    Hi,

    I want to add newsletter block into my one column products page. How to add newsletter block in desired location of the page.

    Thanks

  • Canvas Prints said:

    I am pulling my hair out trying to add a product list to my static block using this code:

    {{block type=”catalog/product_list” column_count=”3″ category_id=”cat_ID” template=”catalog/product/list.phtml”}}

    It simply isn\’t showing, any help is apprectaied

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree

Security Code: