Creating Block from controller in Magento

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.

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

Now write whatever you want to show. Hope this will help you.

Related posts:

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)