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.

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 Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...