Home » Magento tutorials » Inserting your block into any place in Magento

Inserting your block into any place in Magento

You are running a marketing campaign and you want to insert a ads/news block to a specific place on your Magento store but don’t know where to put the code?

In this tutorial, we will go through how to insert block into any place you want in Magento without editing template. We’ll try to explain how to do this using layouts and observers.In our example, for such experiments will be used category page.

Let’s say, you need to place your block inside the other one and this block is instance of the Mage_Core_Block_Text_List class. That means, the block renders his children automatically, what’s easy. Then, you just need to follow the steps which are below:

This block will be displayed after all content of the “left block. To get it displayed before all content – you just need to add the “before” directive. See example below:

If you need to place your block between first level children of the “left” one, you should add before=”name” or after=”name”, and check following code:

Magento Tutorial

It may look simple but when we deal with none first level blocks – supposing, it is necessary to insert the block after price on the category page, and for such implementation you should use the observer core_block_abstract_to_html_before. Besides that, don’t forget to declare model in your config.xml:

The next step is creating the observer and your template file.
You can place the template to app/design/frontend/<package>/<theme>/at.phtml for example. For this, look at the observer’s code below:

And finally, here is a template at.phtml code:

The result is:

Magento tutorials

Source: Atwix

About Gareth

avatar
Gareth is an editor at Tutorial Magento. His special hobby is to share magento knowledge and experience with everyone.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Scroll To Top