Magento Useful Template Snippets

Create a block object inside a phtml template file:

<?php
$myBlockObject = $this->getLayout()->getBlock(“my_block_name”);
echo $myBlockObject->blockFunction();
?>

Insert a phtml template file into another phtml file

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

Get Current Product From Registry

<?php
if( Mage::registry(‘current_product’) ){
$product = Mage::registry(‘current_product’);
}
?>

Get Request (Querystring) Parameter

<?php
$myVar¬†= Mage::app()->getRequest()->getParam(“myVar”);
?>

Get Controller Name
Can use this to check whether you are on a product or a category page

<?php
if($this->getRequest()->getControllerName()==’product’){
if(Mage::registry(‘current_product’)){
//do something useful
}
}
?>

Facebook Comments

Leave a Reply

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

Apply your human brain cells and complete this highly complicated maths problem *