Ways to add custom 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 block on 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="pragmaapps_shopbycolor/shopbycolor_list" name="shopbycolor_list" template="pragmaapps_shopbycolor/shopbycolor/list.phtml"}}
  4. Click on save and Continue edit button. Now the block show on the Home 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="pragmaapps_shopbycolor/shopbycolor_list" name="shopbycolor_list" template="pragmaapps_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.

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('pragmaapps_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.

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="pragmaapps_shopbycolor/shopbycolor_list" name="shopbycolor_list"    template="pragmaapps_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. Add custom block in magento admin programmatically
  2. Magento Mobile Application

Leave a Comment

Scroll to Top