Magento 2 Layout Overview: Containers, Blocks, and Custom Layout Files
The layout system is one of the most important features of Magento 2 or adobe commerce as it allows merchants to create unique and personalized online stores that stand out from the competition. The Magento 2 layout system is highly customizable and can be used to create a wide range of different layouts, from simple and minimalistic designs to more complex and intricate designs.
What is Layout?
Layouts in Magento 2 are XML files that define the structure and content of different pages on the website. Each page in Magento 2 has a corresponding layout file that defines the blocks, containers, and other elements that make up the page.
In image, there is a page with just some lines which are dividing it into different sections, that section is describing the structure of a web page.
By default, Magento provides 5 page layout for the frontend i.e., empty, 1column, 2columns-left, 2columns-right, and 3columns.
And 3 page layout for backend/admin i.e, admin-empty, admin-1column, and admin-2columns-left.
What is Container?
A container is basically a wrapper. A container can hold one or more child blocks, which can be other containers or simple blocks of content. Containers allow developers to structure the layout of a page and control the placement and appearance of its content.
In the above image,?there are different sections like header, footer, left, and main. Basically, these sections are containers, there can be many of the items in each section.
For example,
The header(container) can have logo, navigation link, minicart, etc.
Attribute for Container :
???????For example, <container name=“custom.container” />
???????For example, <container name=“custom.container” htmlTag=“div” />.
???????For example, <container name=“custom.container” htmlTag=“div”
???????htmlClass=“custom-container” />
???????For example, <container name=“custom.container” htmlTag=“div”
??????htmlClass=“custom-container” htmlId=“custom-container” />
???????For example, <container name=“custom.container” label=”Sidebar Additional” />
???????For example, <container as=“product_list_wrapper” />
Sample of container in the layout:
<container?name=”custom.container” htmlTag=”div” htmlClass=”custom-container” htmlId=”custom-container” >
your code goes here…
</container>
What is Block?
Blocks are a foundational building unit for layout in Magento. Blocks can have children and grandchildren (and so on). Information can be passed from layout XML into the block via the <arguments/>.
In the above image, there are many blocks under containers which are highlighted with dark color.
For example:
logo, header links, category filter, compare list, etc are block which are wrapped into respective container i.e., header, footer, main, etc.
Attribute for Block:
???????For example, <block name=“custom.block” />
???????For example, <block class=“Vendor\Module\Block\Class” />
???????For example, <block template=“Vendor_Module::tempate.phtml” />
领英推荐
???????For example, <block cacheable=“false” />
???????For example, <block ifconfig=“contact/contact/enabled” />
???????For example, <block display=“true” />
Sample of Block in the Layout:
<block?class=”Vendor\Module\Block\Class” name=”custom.block” cacheable=”false” ifconfig=”contact/contact/enabled” template=”Vendor_Module::tempate.phtml” />
Common Attribute Used in Container and Block:
How to Create Custom Layout File?
Let’s create a new layout file with the name?new_layout_index.xml.
In the above file name,
new?is the front name of a module
layout?is the name of the controller
index?is the name of the controller class.
Actually, this is a part of?Module development.
Under new_layout_index.xml let’s create a structure of the web page:
<?xml version="1.0"?>
<page xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<container name="new.container" htmlTag="div" htmlClass="new-container" htmlId="custom-container">
<block class="Magento\Framework\View\Element\Template" name="custom.block" template="Vendor_Module::newtemplate.phtml" cacheable="false" />
</container>
</body>
</page>
Output of above layout on browser:
<div?class=”new-container” id=”custom-container”>
?????Content of block goes here…
</div>
Additional Points Which are Used in the Layout:
referenceContainer:?It is used to reference any existing container throughout the layout file. We can use the existing container in our layout by its name within <referenceContainer />.
For example, if we need to add any content in the main section of the page:
<?xml version="1.0"?>
<page xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
????? <body>
??????????? <referenceContainer name="content">
????????????????? <container name="new.container" htmlTag="div" htmlClass="new-container" htmlId="custom-container">
??????????????????????? <block class="Magento\Framework\View\Element\Template" name="custom.block" template="Vendor_Module::newtemplate.phtml" cacheable="false" />
????????????????? </container>
??????????? </referenceContainer>
????? </body>
</page>
referenceBlock:?It is used to reference any existing block throughout the layout file. We can use the existing block in our layout by its name within <referenceBlock />.
For example, if we want to remove product sku from product detail page:
<?xml version="1.0"?>
<page xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
????? <body>
??????????? <referenceBlock name="product.info.sku" remove="true"/>
????? </body>
</page>
What is the Difference Between default.xml and a specific page xml file?
To make changes available on every page, modify the?default.xml?file. For example, if we need to add a custom header and footer so we will modify it in?default.xml. Because the header and footer will be on all pages.
To implement layout changes for a specific page on a website, we utilize a unique layout handle.
For example, if we want to make any changes only for the homepage so we have to change the?cms_index_index.xml?file.
Location for default.xml file in theme:
app/design/frontend/
├── Vendor/
│??│??├──Theme/
│??│??│??├──Magento_Theme
│??│??│??│??├── layout
│??│??│??│??│??|── default.xml
/app/design/frontend/Vendor/Theme
/Magento_Theme/layout/default.xml
Note:?default.xml?file can be used in any layout folder either under Theme or Module.
Here are a few example for specific page: