Ways to add custom block on magento

adding block on magento

Recently we have created our custom block for eCommerce website built in Magento. Our client wants this block on Home page, phtml file and layout xml. After so many research, we found that there are so many ways to show our custom block on magento site. In this article we demonstrate how we can show or adding custom blocks magento. Below is the Ways to show blocks on magento :

Step 1: Add custom blocks on Any Magento CMS page

Adding a custom block to Magento CMS page is a very good way to improve the flexibility of these static pages. Show block on CMS page is very easy. You just need to define the custom block on any CMS page. There is two method to display Custom block on Magento CMS page :
1. Define custom block on the CMS Content area

  1. Go on the Admin > CMS > Pages > Home Page.
  2. Click on Content from the Page Information section.
  3. Define our custom block in Content in below format
    {{block type="ipragmatech_shopbycolor/shopbycolor_list" name="shopbycolor_list" template="ipragmatech_shopbycolor/shopbycolor/list.phtml"}}
  4. Click on save and Continue edit button. Now the block show on the Home page.

custom-block-cms-page

2. Define custom block on the CMS Design area

  1. Go on the Admin > CMS > Pages > Home Page
  2. Click on Design from the Page Information section
  3. Define our custom block on Page Design in below format
    <reference name="content">
                <block type="ipragmatech_shopbycolor/shopbycolor_list" name="shopbycolor_list" template="ipragmatech_shopbycolor/shopbycolor/list.phtml" />
    </reference>

    Reference name=”content” means that the blocks or other references inside those blocks will be available in content block on your theme templates.

  4. Click on save and Continue edit button. Now the block show on the Home page.

custom-block-cms

Step 2: Programmatically add custom block in Magento

See the below code how we can add custom block on the Magento admin controller programmatically.

class Ipragmatech_Adminblock_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action{
 
    public function indexAction(){
                $this->loadLayout()
                ->_addContent(
                $this->getLayout()
                ->createBlock('ipragmatech_adminblock/adminhtml_adminblock')
                ->setTemplate('inchoo/shopbycolor.phtml'))
                ->renderLayout();
    }
}

Now visit yourvirtualhost/admin/index/index and you can see that the job is done!. Shop By Color block is visible on the admin controller.

code-structure

Step 3: On Layout xml

We can also define block on the any module layout xml. Go on the app > design > frontend > yourtheme > layout > yourmodule_extension.xml and paste the below code to display block on the layout xml.

<reference name="left">
    <block type="ipragmatech_shopbycolor/shopbycolor_list" name="shopbycolor_list"    template="ipragmatech_shopbycolor/shopbycolor/list.phtml" />
</reference>

Now visit the http://yourhost and you can see that the job is done!. Shop By Color block is visible on the home page.

Conclusion

Display the block on frontend is very important in every website. Hence, by using these ways we can display custom block on anywhere in the magento website.

References

  1. http://inchoo.net/magento/programmatically-add-custom-block-in-magento-admin/
Ways to add custom block on magento was last modified: March 8th, 2016 by Ajay Mehta