How to add a template file directly into another template (.phtml)

Posted by Labels: at

Sometimes it is not easy to move template blocks around your Magento store using local.xml, so here is a short code that can help you achieve the same result.


<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('newsletter/subscribe.phtml')->toHtml();?>


CASE STUDY:
Lets say you want to move the newsletter subscribe template from the footer and only show it on the 2columns-left.phtml layout template but in a different location.

1. First you need to remove the newsletter/subscribe.phtml from all you layout pages using local.xml.


<?xml version="1.0"?><layout>    
    <default>
           <remove name="footer.newsletter"/>
     </default>
</layout>


2. Open your layout template, 2columns-left.phtml

File Path:
app/design/frontend/package_name/theme_name/template/page/2columns-left.phtml

3. Place the following script any where you would like to display you newsletter subscriber template.


<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('newsletter/subscribe.phtml')->toHtml();?>


As you can see, the function setTemplate('newsletter/subscribe.phtml')->toHtml()
takes in the template file subscribe.phtml located in your template's newsletter folder.

File Path: 
app/design/frontend/package_name/theme_name/template/newsletter/subscribe.phtml

You maybe be wondering how did i know that the newsletter subscribe block has a name of footer.newsletter the block name i used on local.xml to remove all newsletter subscriber templates from layouts? Well, Fortunate for you, my next topic is, "How to find out the block name of a template file (Path Hint)".

I hope this article was helpful to you, please leave your question/s on the comments section below and I will do my best to answer back.






Post a Comment

Back to Top