Question

In Magento 2, some layout handle XML files open with the

<page.../>

root tag. Others open with the

<layout.../>

root tag. Does using either of the above tags introduce any functionality differences to a layout handle XML file? Or is this pure window dressing? Or something in between.

Two examples

<!-- File: vendor/magento/module-weee/view/frontend/layout/default.xml -->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

and

<!-- File: vendor/magento/module-widget/view/adminhtml/layout/adminhtml_widget_loadoptions.xml -->
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">
Was it helpful?

Solution

These matters due to a different XSD configurations. The

<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout_generic.xsd">

declaration says that we want to use layout with XSD definition provided in the

lib/internal/Magento/Framework/View/Layout/etc/layout_generic.xsd

In the layout_generic.xsd file it provides definition for layout node with genericLayout element type.

<xs:complexType name="genericLayout">
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="referenceContainer" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="container" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="block" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="referenceBlock" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="update" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="move" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element ref="uiComponent" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

Having that, the layout file has been validated by the XSD when xml is loaded. In addition to this it highlights all possible nodes and node attributes in the layout.xml file.

Same with page_configuration.xsd definition for <page /> node. This XSD describes nodes which can be used to describe page.

Hope it helps.

OTHER TIPS

Layout files which open with <layout></layout> are page layouts that declare the wireframe of a page inside the <body> section, for example one-column layout or two-column layout.

The layout files which open with <page></page> are page configuration files that add content to the wireframe defined in a page layout file.

Here is the official doc about it, please let us know if it can be improved: http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/layout-types.html

One difference that I've noticed is, because is wireframe of a page inside body, you can ajax retrieve just a part of html. Normally if you hit a controller you get the whole page starting with tag. These controllers are returning page objects created by PageFactory which uses type layout xml. But when you want to dynamically load some phtml with ajax call like filter form, is helpful. Magento_ImportExport dynamically changes export filters when entity (product,customer) changes. This is an example of when magento uses (adminhtml_export_getfilter.xml).

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