Using Custom phtml Template Files Rather Than Static Blocks in Magento

Magento’s CMS static block system strips out code it doesn’t like causing problems for developers who would like to add for example a javascript feed in areas like the menu or header. Or perhaps Rich Snippets aggregate ratings code on specific pages like the homepage – as in the below example. This means you have to hardcode in the necessary script. This itself causes problems due to the modular nature of Magento, meaning that the file structure is so huge and confusing that it is difficult to find files.

In this example I will explain how to add this areas of code Magento doesn’t like to the home page.

Rather than putting the offending code in a static block (like for example in this previous post) you need to put the necessary code in phtml files – otherwise Magento will modify it rendering it useless.

  • Set up a phtml file with the code in… in this case I’m using this process for setting up    the rich snippet code in a file called reviews.phtml – (obviously the file can be called whatever you want and can contain any code you like)
<p><span itemscope itemtype="http://schema.org/Product">
<span itemprop="name"><strong>The Promotional Gifts Company</strong></span> is
<span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
rated <span itemprop="ratingValue">5</span> / 5<br />
based on <span itemprop="reviewCount">5</span> <a href="http://www.reviewcentre.com/Online-Gift-Shops/The-Promotional-Gifts-Company-promotional-gifts-co-com-reviews_1490368">customer reviews</a></a> </span>
</span>on ReviewCentre.com</p></span>
  • Simply put this file in your page folder:(public_html/app/design/frontend/default//YOUR_TEMPLATE/template/page/)
  • Now all you need to do is to call the file from a CMS page using this code:
<p>{{block type="core/template" template="page/reviews.phtml"}}</p>

To call the custom template from another phtml file rather than a CMS page use this code:

<?php echo $this->getLayout()->createBlock(‘core/template’)->setTemplate(‘page/reviews.phtml’)->toHtml(); ?>

Hope this helps someone!

Leave a Reply

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

Previous Post

Adding A Static Block to Your Magento Menu

Next Post

Configuring an Adsense Account on Multiple Analytics Profiles

Related Posts