Today, we will explain how to remove blocks or containers called by layout files.
What Are Blocks & Containers in Magento 2?
Magento 2 uses a layout XML system to build the structure of every page. This structure is mainly composed of blocks and containers.
Containers
- Containers define the page structure
- They control where content appears
- They do not render HTML themselves
- Examples:
header.containercontentfootersidebar.main
Containers act like holders or wrappers for blocks.
Blocks
- Blocks are responsible for rendering actual content
- They output HTML to the frontend
- Usually linked to
.phtmltemplates or UI components - Examples:
- Product list
- Mini cart
- Newsletter subscription
- CMS static blocks
Blocks live inside containers and display data to users.
Remove Blocks or Containers?
Firstly, you need to create a layout file for the same controller.
for example: catalog_category_view.xml
Now add the line in your layout file:
<referenceBlock name="your block name" remove="true" />
In this case, you can use referenceBlock for the blocks and referenceContainer for the containers.
“name” is the name of the block or container that you want to remove.
“remove” is set to true to remove.
Your file will be like this:
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="content" remove="true" /> </body> </page>
After flushing the cache, the whole content container will be removed from the category page.
Removing Blocks or Containers Safely — Best Practices
Use a checklist:
- Do not edit core Magento
- Use a custom theme or module
- Test with caching ON
- Avoid removing JS-critical containers
Hope this will help you add functionality to your module.
I hope this blog will help you Remove Blocks or Containers from the Layout of Magento 2. You may also check our wide range of best Magento 2 Extensions.
Please reach out to our team via a support ticket if you have any queries.
Try this, and if you have any queries, then just comment below 🙂
For technical assistance, please get in touch with us via email at [email protected].
Discover powerful solutions to enhance your Magento 2 store by exploring our Magento 2 plugins page.
Bring your vision to life with custom-built solutions—hire skilled Magento 2 developers today.
Happy Coding!!
Be the first to comment.