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’);
}
?>

Insert static block into phtml file

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

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 *