Magento Static Blocks – The Definitive Guide

Magento Static Block
There’s more than one way to skin a hippo and adding CMS static blocks in Magento is no exception.
In case you’re unfamiliar with CMS static blocks, they are powerful little buggers in Magento’s admin that allows the site’s administrator to add and control chunks of HTML that can be displayed throughout the site. They’re perfect for seasonal banners, sale blocks, return policies, size charts and anything that would make sense to modularize to make maintaining your site easier.
But wait, aren’t there already ‘callouts’ in Magento? Well, if you’re talking about those annoying graphics of the dog and chalkboard that take editing multiple files to update then yes. Magento’s built-in callouts are a terrible way of handling regularly updated content.
Your Magento website should be as updatable as possible to keep you from getting phone calls every time a client wants to advertise a new sale. Which is exactly why we want to control these blocks from the admin. Keep in mind Magento’s upcoming release of 1.4 will be implementing a WYSIWIG editor so clients can handle their own changes instead of pestering you.
Creating a Static Block
- Log into your Magento store’s admin
- Navigate to CMS>Static Blocks
- Click Add New Block in the top right corner
- Give your block a recognizable Block Title such as Social Media Links or “Fall Sale Banner”
- Give your block an Identifier which will be used to call the block. Make sure the Identifier is all lowercase and separated by underscores to follow Magento’s nomenclature i.e. your_block_id
- Choose what store view the block belongs to. Just leave as All Store Views unless you have a good reason not to
- Set Status to Enabled
- Enter your HTML in the Content field. The editor is currently a raw HTML editor, but 1.4 will support a WYSIWIG editor. Alternately, there is a Magento WYSIWIG extention to help out.
- Click Save Block or Save and Continue Edit to save your settings.
You’ve set up your block, so how do you plug it into your site? Well it depends on how you need it to function, but you have several options at your disposal:
1. XML
Adding a static block to a page template is a great way to control global elements of your site, such as footer links, custom callouts in the sidebar (ultimately replacing that damn dog) and more. You can embed this code in app > design > frontend > default > your_theme > layout. Open the appropriate the file, lets say catalog.xml and plunk the following code in the
<action method="setBlockId">your_block_id</action>
</block>
This code will place the block “your_block_id” that you have created in the admin above the content on the category pages (notice the before=”-” attribute, which makes sure your block gets displayed before the rest of the content). This is perfect for a seasonal banner that could advertise a current sale on all product listings.
Controlling static blocks with XML is geared for content that will remain in a consistent position in your theme.
Sometimes however you gotta get down and dirty and place your CMS static block inline in your template. That’s where the next method comes in.
2. PHP
Adding your static block inline with PHP is the quickest way to get your block in your template. Let’s say you want to add a quick blurb about your return policy right after the “Add to Cart” button. The client needs to be able to occassionaly update this blurb from time to keep it current. So you open your template file that contains the “Add to Cart” button app > design > frontend > default > your_theme > template > catalog > product > view > addtocart.phtml. Find the <button> tag and right afterwards add the following code:
This code will add the block “your_block_id” right after the button. Jobs done. This method is perfect for getting into those nooks and crannies in Magento’s vast and awkward file structure.
3. Shortcode
This method is used when you need to pull in a static block while in Magento’s admin creating CMS pages or other static blocks. A possible example would be injecting contact information into multiple CMS pages. So you create a contact static block, and then can insert the contact info on the contact us page, your privacy policy page, customer service page, etc. If the contact info changes, you simply update the static block and the changes will be reflected across all your CMS pages.
This code will place the block “your_block_id” inline in your CMS page.
Conclusion
The whole idea of creating these static blocks is to streamline the amount of time it takes to update your site. Clients won’t have to bother you to change their 800 number. Your design team or site administer can simply FTP a new image and update the image path. Or if you own the site, you don’t have to go dumpster diving through your template files to find where you put that couple paragraphs of content.
Do you have a preferred method of adding a static blocks or creative uses of static blocks in your Magento site? Let me know in the comments.
11/18/09
What a superb article! Thanks a lot for sharing this information – it makes perfect sense.
Right, I’m off to create some static blocks!
11/18/09
We’re fans of letting clients drive the content-inclusion bus.
We’ll set up a dropdown attribute for them to use, the values of which correspond to block identifiers. That way for each product they can choose a CMS-managed message to be displayed on the product page, and they can add blocks to the list at will. Example usage: delivery restrictions, availability notes, etc.
This employs (in part) the PHP method you noted above.
11/18/09
@Ben, That’s a beautiful example of the power and flexibility of working with static blocks. Giving the client the ability to choose from a dropdown keeps their dirty mitts off any raw HTML while keeping it as flexible as possible. I’d love to see some examples of the method!
11/18/09
Great article Brad!
I use the PHP method to include blocks for customers, the most popular being sidebar marketing blocks, one page checkout block and purchase success page block.
Also, using a static block is a quick way to add client editable content to the Magento built-in contact form that stays put on the other side of the form submit.
11/18/09
Thanks Daniel. I’ve definitely found that the contact static block you’re referring to is one of the most important blocks to remain editable. Clients always seem to be editing or adding to the contact information. Also, the purchase success page block is genius, its always such a pain to design that page since it by default redirects you and you can’t get back to it. Extremely good idea.
11/25/09
Brad,
Thanks for this clear explanation.
I was just implementing the php version (no. 2) and noticed that it seemed to be lacking the closing “?” in the tag. I don’t know PHP well but when I pasted the code (with my block id) in place I got an error and noticed the missing mark.
11/25/09
Thanks for the catch David. I’ve updated the code to include the closing question mark
11/25/09
Great article, tho i already are familliar with static blocks.
i just have one problem.
if i make a static block and in that code has program that neeed files and pictures, where do i put this stuff,
fx:
i have a php script m that make greethingcards, so my idea vas to put the
code from the index php in the static bloks, and add the files to same place,
just dont know where to put the stuff.
best regards ole dahl
11/25/09
Hey Ole,
Thanks for commenting. It sounds like what you’re trying to accomplish is beyond what CMS static blocks can do. Static blocks won’t execute PHP scripts so I would assume you would have to write some PHP files within your template to accomplish your desired functionality. If you’re wondering where to put images and other assets, typically I will keep them in my theme’s skin folder i.e. /skin/frontend/default/your_theme/images/media/your_image.jpg and then just link to them from within my static blocks
11/25/09
ok tx. guess i have to build extension for it to work
11/30/09
Is there a way to use the short block (or other syntax) to include a static block through the Admin -> Manage Products -> Description textarea? When I use the short block notation you describe it is not replaced with my static block but is displayed as text. Would it be possible through a Custom Layout Update in the Design section? Thanks, Ken
12/02/09
OK – I have found out I cannot use the {{…}} syntax in descriptions, etc. I have also found multiple – and inconsistent – syntaxes for adding <block type=”cms/block”… to the xml files. I added the following to catalog.xml right after the name=”product.description” block – but nothing shows up in the resulting page:
haoverview
This seems to be the most widely documented syntax (variants are w & w/o as=, after=, before=, , , etc). Nothing seems to work.
I think I am in the correct file because if I comment out the product.description block it disappears from the generated page.
Can anyone help?
12/02/09
Great article Brad.
I managed to use your example number 2, but I can’t for the life of me get number 1 working at all? Is there a trick to this or can it only be used in certain places?
I would like to create static blocks for certain promotions, so I can display things like “Buy this item and get a free X” on the product detail page. How would you go about doing this and is it actually possible?
Thanks again for a great article.
12/02/09
I should add that I am trying to do this on specific products using the “Custom Layout Update” field in the “design” tab when adding/editing a product.
12/21/09
Oups. My comment above was messed up with tags escaping. So without tags:
There is missing one tag in the xml code example – block_id tag around your_block_id within action tag.
01/06/10
good work bro! now I compleatly understand static blocks. The only thing I want to work out now is working out how to display differnt blocks in the sidebar based on different cat_ids or parent cat_ids.
Can anyone help? Thanks in advance