Web Development Blog

How to Add Dynamic Content to Magento Static Blocks and Pages

03.03.2009 10:06PM by Evan Johnson

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.

Comment

Gravatar Image
On May 1, 12:29 AM, (Link)
Mark said:

Hi Evan,

Please can you help ? You mention in this article “I wrote a small custom module” Is this available, if so where?

I have tried to implement JakeRutters example on Magento v1.3 but can’t get it to work :o(

So if you have the module or code available I would really appreciate it.

Thanks in hope :o)
Mark

Gravatar Image
On May 1, 12:45 AM, (Link)
Mark said:

Oh and one other thing. There seems to be alot of other people wanting thumbnails for sub categories displayed. So if you did do a module it would be popular and people may even pay for it, i would :o)

Cheers
Mark

Gravatar Image
On May 1, 03:05 PM, (Link)
Evan Johnson said:

@Mark
I think the 1.3 Magento upgrade DID break Jake Rutter’s original code. There might be more to it than this, but I distinctly remember that for 1.3 I had to change this line of code:

<?php if($_categories->count()):?>

to this:

<?php if(count($_categories)):?>

Give that a try. The whole “flat catalog categories” thing in 1.3 broke some other custom code I had though, so it might be more involved if you have that option enabled.

Thanks for the positive support about making a category thumbnail module… perhaps I will. Perhaps I will. I’ll post about it here if I do.

Cheers!

Gravatar Image
On May 22, 07:00 AM, (Link)
x4hai said:

I use Free CMS module, it’s very useful. You can see the demo here: http://demo.hello-magento.com/freecms/furniture.html

Gravatar Image
On Jul 31, 03:38 PM, (Link)
bhoot said:

Many thanks for this tip. Works great ;) And better control than FreeCMS, I’d say because it’s within Magento.

Gravatar Image
On Feb 21, 11:53 PM, (Link)
Jakob said:

Can you please explain all the params u used? I furthermore would like to have my own Params, is there a way to pass them to my php script without ugly links? I am thinking about doing modifications in the .htaccess file.

Regeards

Gravatar Image
On Feb 22, 09:52 PM, (Link)
Evan Johnson said:

@Jakob – The params I’m using are:

type – I don’t remember what the other options are (I think they vary by module, and you can create new ones when you create a new module), but "core/template" is a basic "type" that you can use for simple custom html/php.
name – this is just a name (you can call it anything) you use to refer to the block after you’ve created it. Usually you use this in Layout XML to determine block order. Not really important in this use case.
template – This is where you call the actual file name of the dynamic PHP content you want to insert into the static block or page.

I am not sure about passing parameters to the block, although that would certainly be useful. Have you tried something like template='newsletter/subscribe.phtml?myparam=myvar'? I’m pretty sure it won’t work, but that’s the only thing I can come up with right now. Good luck, and I’d be curious to learn how to do it as well, so if you figure it out please post about it here!!!

Thanks and cheers

Gravatar Image
On Mar 10, 02:46 AM, (Link)
Sumanta Pati said:

I am unabale to get the dynamic content of a CMS block. If I use there some static data or text, then I can view it in the frontend. But if I use something like
{{block type='core/template' name='contactForm' template='newsletter/subscribe.phtml'}}
Then I get nothing in the frontend. I am using the version magento-1.4.0.0
Please help me by replying.
Thanks.

Gravatar Image
On Mar 10, 10:03 AM, (Link)
Evan Johnson said:

@Sumanta Pati – Your problem might be that you are calling the wrong block "type". When I look at the layout XML I see that the newsletter/subscribe.phtml block is of the type "newsletter/subscribe" but you are using the "core/template" type. Try this and see if it works: {{block type='newsletter/subscribe' name='contactForm' template='newsletter/subscribe.phtml'}}

Gravatar Image
On Mar 12, 06:16 AM, (Link)
Sumanta Pati said:

Thank You Very much for your help.
Now its work properly.

Gravatar Image
On Apr 9, 05:22 PM, (Link)
Jake Rutter said:

Evan,

Thanks for the plug! Its great to see the community helping each other out!

Gravatar Image
On May 11, 12:26 PM, (Link)
SEM Truth said:

I have been using the <a href="{{store url=""}}"> function however it appears that with the latest release of Magento 1.4.0.1 you cannot simply update the code any longer to allow for dynamic links leading to category pages such as

<a href="{{store url="single-rolls.html"}}">

This will end up linking to storeurl.com/single-rolls.html/ which is not the appropriate page.

Also this setup no longer works within CMS Blocks or Static Pages:
<a href="{{store url=""}}single-rolls.html">

Has anyone found a work around for this.

Gravatar Image
On Jun 3, 05:28 AM, (Link)
prabeen said:

Nice tutorial………..It is really helpful.

Gravatar Image
On Dec 1, 02:02 AM, (Link)
termit said:

Thanks! Great tutorial!

Gravatar Image
On Sep 22, 03:34 AM, (Link)
Manan said:

Thanks ! Nice tutorial!

  Subscribe to article comments