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.
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments