Magento Themes and Magento Extensions

Creating Block from controller in Magento

Share

There are two types of blocks can be created in Magento

a) Structural Block: This block usually contains the attribute ‘as’ with block tag through which the application will communicate with designated area.

b) Content Block: These are the blocks that produces actual content inside each structural block. It seems like normal template file that are used to show particular data on the page.

Most of the time a programmer needs to create a structural block and need to show it into the content area of the page. Here are the code you need to write for it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Public function TestAction()
{
//Loading current layout
$this->loadLayout();
//Creating a new block
$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'test_block_name',
array('template' => 'page/html/testblock.phtml')
);
$this->getLayout()->getBlock('content')->append($block);
//Now showing it with rendering of layout
$this->renderLayout();
}

Now we will create  a file into your design/frontend/default/default( or your theme name)/page/html/testblock.phtml

Comments are closed.