Question

In magento2 Layout updates are usually spread accross multiple files in your custom theme. e.g.

Magento_Catalog/layout/catalog_product_view.xml
Magento_Theme/layout/default.xml
Magento_Customer/layout/customer_account.xml

Is it good practice to put everything in Magento_Theme/layout/default.xml, and if not why?

For instance you could have have the below all in default.xml:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="register-link" remove="true" /> <!-- This occurs on every page -->
        <referenceBlock name="customer-account-navigation-downloadable-products-link" remove="true"/> <!-- This occurs on only on the account pages -->
        <referenceBlock name="product.info.review" remove="true" /> <!-- This occurs on only the product details page -->
    </body>
</page>

If everything is put in one file, the default.xml, perhaps it's overkill as not all instructions are required for each page view or is the same layout file compiled and loaded for each page view, in which case it would make no difference.

Was it helpful?

Solution

The default.xml layout file is used in order to declare some layout instructions which are trigger on all pages.

If you need specific instruction for specific request, you need to put in the specific handle layout file.

The final layout file is generated and merged internally by Magento for each request, and so the default.xml instructions are always added to the merged result file.

Moreover, the default.xml can be located in the Theme module, but also in other module such as Catalog etc... All module layout files are parsed and merged in one file internally.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top