Magento: Call block and template

In Magento each view page contains template(html) and its own block (PHP) so that understanding about template, block is beginning of Magento web application.

A. You can see template, block path in front-end by enable it from admin panel as following steps.

1. Go to system > configuration
2. Switch store from default to main website
3. Click on developer in advanced tab
4. Click on debug tab in right side panel
5. Select yes in template path hints
6. Select yes in Add Block Names to Hints

Then save configuration.

Refresh front-end page for displaying path of template and block.

B. You can see template, block path by enable it in code.

1. Go to below file
app/code/core/mage_Core_Block_Template.php

2. Change code from
protected static $_showTemplateHints;
protected static $_showTemplateHintsBlocks;

to

protected static $_showTemplateHints=true;
protected static $_showTemplateHintsBlocks=true;

Then refresh page to display path of template and block.

Call block and template in various ways:

<?php 
echo $this->getLayout()->createBlock('googlecheckout/link')->setTemplate('googlecheckout/link.phtml')->toHtml(); 
?>

Call block and define template in block


<?php
$this->getLayout()->createBlock('catalog/breadcrumbs');
?>

The more I use this code snippet the more I keep having to dig this back up, so I’m posting it here for reference for others as well. Here’s the quick and easy way to insert CMS Blocks into template (.phtml) files without any layout modifications:


<?php 
echo $this->getLayout()->createBlock('cms/block')->setBlockId('your_static_block_id');
?>

By defining in layout :

<block type="youtube/product_view_youtube" name="product.youtube" as="youtube" template="catalog/product/view/youtube.phtml"/>

Call in template :


<?php 
echo $this->getChildHtml('youtube') 
?>

Enjoy!!!!!