Menu Close
    Hire us Request quote Reading list Switch to dark mode
    lockdownWebkulUp

    Ways to set / unset Blocks in Magento

    Today I am going to share how we can Add Remove Block in Magento 2.

    I hope you have basic knowledge about how to create module is Magento.
    If you are new to Magento then first go to thing link.

    Magento Follows MVC design pattern and I found that people are generally confused about layout and view system.
    and block is a part of the View system. In magento View is separated into two parts, One is Block and another is Template.

    What is Block ?
    Block is a simple Object tied to Template file.

    What is Template ?
    Template is a Raw php file (having the extention .php) contain php and html code. In phtml(template) file $this keyword keeps the reference of the template’s Block Object.

    We can perform block nesting as well. Block into Block.
    Basically block inside block inside block is how Magento html layout is create for page.
    For performing Nesting we use a method getChildHtml method.

    getChildHtml help us to call any block in any place in particular page scope.
    Example :

    Call specific block $this->getChildHtml(“head”);

    All child Blocks $this->getChildHtml();

    There are lots of terms that you show know, like layout packaging, references , handles etc,

    but I can not cover all in one  as I need to explain a  thing that you may need when you are creating any module  and situation came and you want to remove the block from the layout and add new block in layout.

    To add/remove block in layout we need to get the reference of the layout Object. We use getLayout() method to get it’s reference.

    There are generally two ways to remove the block from the layout :
    1. XML
    2. PHP Code.

    . . .
    Subscribe Articles

    Just enter your email below to subscribe and receive weekly updates about the most interesting similar articles.

    You need to accept conditions to receive letters.
    You've Subscribed!

    Share article via

    . . .

    First let me cover the XML part .
    Remove / add Block :


     

    Add/Remove Block  (PHP):




    Note : You can use removeBlock() function as well .

    Note : you can write the above code is block as well as in Template , it works in both places.

    Note : Best Practice, we should use this code in Block’s prepareLayout() function.

    Situation : When to use in Template file
    Example : parent block has used the function getChildHtml() at the bottom and you want your block to be tied in between(middle of the page/block).

    Scenario : if you will add the line after overriding the template in between the Template, so the same block will come twice so you have to remove the bottom occurrence of the block.

    Few more function in view object :
    1. setTemplate
    2. unsetTemplate
    3.
    This code will help you to come out of this situation.

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close