Magento provides an easy way to add blocks with using Layout-XML file and also call directly from the phtml file and added to any category page with no programming at all.
Adding Magento Static block:
from XML:
1 2 3 4 5 |
<block type="cms/block" name="your_block_name" before="-"> <action method="setBlockId"><block_id>your_block_id</block_id></action> </block> |
From phtml/php :
1 2 3 |
$this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_id')->toHtml() ; |
From the CMS page content/Shortcode way:
1 2 3 |
{{block type="cms/block" block_id="your_block_id"}} |
Adding Magento dynamic block:
From XML:
1 2 3 4 5 6 7 |
<reference name="content"> <block type="catalog/product_list" name="mobileproduct" template="catalog/product/list.phtml"> <action method="setCategoryId"><category_id>2</category_id></action> </block> </reference> |
From the CMS page content/shortcode way:
1 2 3 |
{{block type="catalog/product_list" category_id="2" template="catalog/product/list.phtml"}} |
From phtml/php:
1 2 3 4 5 6 |
<?php echo $this->getLayout()->createBlock('catalog/product_list')->setTemplate('catalog/product/home-list.phtml')->setCategoryId('2')->toHtml(); ?> |
or if the block is already existing in the layout you can get it by name:
1 2 3 4 5 |
<?php echo $this->getLayout()->getBlock('blockname')->setTemplate('catalog/product/home-list.phtml')->setCategoryId('2')->toHtml(); ?> |
Note that all functions available to you within the layout XML via are also available to call on any block in our code. For example, if we instantiated a new block to be added / appended to the head, we could execute something similar to this:
1 2 3 4 5 6 7 |
<?php $block = $this->getLayout()->createBlock('yourmodule/block', 'blockname',array('template' => 'demac/doubleclick/example.phtml') ); $this->getLayout()->getBlock('head')->append($block); ?> |
Call Magento Block method/function:
There are two syntax to call block method/function as below.
1 2 3 4 5 |
<?php $_blockData = $this->getLayout()->getBlockSingleton('yourmodule/blockname')->getFunctionName(); ?> |
or
1 2 3 4 5 |
<?php $_blockData = $this->getLayout()->createBlock('yourmodule/blockname')->getFunctionName(); ?> |
Example :
1 2 3 4 5 |
<?php $_blockData = $this->getLayout()->getBlockSingleton('catalog/navigation')->renderCategoriesMenuHtml(0,'level-top'); ?> |
It shows the category menu.
or,
1 2 3 4 5 |
<?php $_blockData = $this->getLayout()->getBlockSingleton('catalog/navigation')->getCurrentChildCategories(); ?> |
Adding Magento Content Blocks:
Add
1 2 3 4 5 6 7 |
<helloworld_index_index> <block type="page/html" name="root" output="toHtml" template="helloworld/simple_page.phtml"> <block type="customer/form_register" name="customer_form_register" template="customer/form/register.phtml"/> </block> </helloworld_index_index> |
We’re adding a new Block nested within our root. This is a Block that’s distributed with Magento, and will display a customer registration form. By nesting this Block within our root Block, we’ve made it available to be pulled into our simple_page.html Template. Next, we’ll use the Block’s getChildHtml method in our simple_page.phtml file.
Edit simple_page.html so it looks like this
1 2 3 4 5 |
<?php echo $this->getChildHtml('customer_form_register'); ?> |
Clear your Magento cache and reload the page and you should see the customer registration form.