Insert a Custom Block into Magento Template File

As apposed to creating / adding a block to one of your Magento theme files, working directly in one of the template or .phtml files is a better way to call a static block.

In continuing to develop and learn Magento I have made a number of discoveries that help clarify how to work within the setup of Magento. In most cases when looking to customize a given page, I was directed to making updates to the Layout .xml files.

However if I was looking to simply add a static block to a specific section of a template file, creating a static block to the appropriate .xml file would simply add the static block above or below a given area.

The following code for example, would add the contact_intro above my contact form, but in the setup of that page, I wanted the contact_intro to integrate into a specific section of the fonrm.phtml template. This was not possible by updating the contacts.xml file as follows:

<reference name=”content”>
<block type=”cms/block” name=”contact_intro”>
<action method=”setBlockId”><block_id>contact_intro</block_id></action>
</block>
<block type=”core/template” name=”contactForm” template=”contacts/form.phtml”/>
</reference>

Instead I needed to update the form.phtml file directly. Where I could place the contact_intro directly into the setup of the contacts page html:

<?php echo $this->getLayout()->createBlock(‘cms/block’)->setBlockId(‘contact_intro’)->toHtml() ?>

Now I do not have two seperate blocks, when trying to add a block to the layout file. I have placed the static block exactly where I want it to be called within the contacts page.

I hope this helps clear things up as it has for me when trying to accomplish certain Magento layout updates.

Speak Your Mind

*

CAPTCHA image
*