Magento 2 layout instructions

Changing layout files is one of the two possible ways to customize page layout in Magento 2 (the second way is altering templates). To change the page wireframe, modify the page layout files; all other customizations are performed in the page configuration or generic layout files.

Use layout instructions to:

  • move a page element to another parent element
  • add content
  • remove a page element

The basic set of instructions is the same for all types of layout files. This article describes these basic instructions; for details about how they are used in particular layout file type, please refer to the Layout file types article.

Magento 2 common layout instructions

<block>

Defines a block.

Details: A block is a unit of page output that renders some distinctive content – a piece of information, a user interface element – anything visually tangible for the end-user. Blocks employ templates to generate HTML. Examples of blocks include a category list, a mini cart, product tags, and product listing.

AttributeDescriptionValuesRequired?
className of a class that implements rendering of a particular block. An object of this class is responsible for actual rendering of block output.class nameyes
nameName that can be used to address the block to which this attribute is assigned. The name must be unique per generated page. If not specified, an automatic name will be assigned in the format ANONYMOUS_n0-9, A-Z, a-z, underscore (_), period (.), dash (-). Should start with a letter. Case-sensitive.no
beforeUsed to position the block

before an element under the same parent. The element name or alias name is specified in the value. Use dash (-) to position the block before all other elements of its level of nesting. See before and after attributes for details.

Possible values: element name or dash (-)no
afterUsed to position the block after an element under the same parent. The element name or alias name is specified in the value. Use dash (-) to position the block after all other elements of its level of nesting. See before and after attributes for details.Possible values: element name or dash (-)no
templateA template that represents the functionality of the block to which this attribute is assigned.template file nameno
asAn alias name that serves as identifier in the scope of the parent element.0-9, A-Z, a-z, underscore (_), period (.), dash (-). Case-sensitive.no
cacheableDefines whether a block element is cacheable. This can be used for development purposes and to make needed elements of the page dynamic.true or falseno

To pass parameters use the <argument></argument> instruction.

<container>

A structure without content that holds other layout elements such as blocks and containers.

Details: A container renders child elements during view output generation. It can be empty or it can contain an arbitrary set of <container> and <block> elements.

AttributeDescriptionValuesRequired?
nameA name that can be used to address the container in which this attribute is assigned. The name must be unique per generated page.A-Z, a-z, 0-9, underscore (_), period (.), dash (-). Should start with a letter. Case-sensitive.yes
labelAn arbitrary name to display in the web browser.anyno
beforeUsed to position the container before an element under the same parent. The element name or alias name is specified in the value. Use dash (-) to position the block before all other elements of its level of nesting. See before and after attributes for details.Possible values: element name or dash (-).no
afterUsed to position the container after an element under the same parent. The element name or alias name is specified in the value. Use dash (-) to position the block after all other elements of its level of nesting. See before and after attributes for details.Possible values: element name or dash (-).no
asAn alias name that serves as identifier in the scope of the parent element.0-9, A-Z, a-z, underscore (_), period (.), dash (-). Case-sensitive.no
outputDefines whether to output the root element. If specified, the element will be added to output list. (If not specified, the parent element is responsible for rendering its children.)Any value except the obsoletetoHtml. Recommended value is 1.no
htmlTagOutput parameter. If specified, the output is wrapped into specified HTML tag.Any valid HTML 5 tag.no
htmlIdOutput parameter. If specified, the value is added to the wrapper element. If there is no wrapper element, this attribute has no effect.Any valid HTML 5 <id> value.no
htmlClassOutput parameter. If specified, the value is added to the wrapper element. If there is no wrapper element, this attribute has no effect.Any valid HTML 5 <class>value.no

Sample of usage in layout:

...
<container name="div.sidebar.additional" htmlTag="div" htmlClass="sidebar sidebar-additional" after="div.sidebar.main">
    <container name="sidebar.additional" as="sidebar_additional" label="Sidebar Additional"/>
</container>
...

This would add a new column to the page layout.

before and after attributes

To help you to position elements in a specific order suitable for design, SEO, usability, or other requirements, Magento software provides the before and after layout attributes.

These optional attributes can be used in layout XML files to control the order of elements in their common parent. The following tables give a detailed description of the results you can get using the before and after attributes. The first table uses a block a as positioned element.

AttributeValueDescription
beforeDash (-)The block displays before all other elements in its parent node.
before[element name]The block displays before the named element.
beforeempty value or [element name] is absentUse the value of after. If that value is empty or absent as well, the element is considered as non-positioned.
afterDash (-)The block displays after all other elements in its parent node.
after[element name]The block displays after the named element.
afterempty value or [element name] is absentUse the value of before. If that value is empty or absent as well, the block is considered as non-positioned.

Examples

SituationResult
Both before and after attributes are presentafter takes precedence.
Both before and after attributes are absent or emptyThe element is considered as non-positioned. All other elements are positioned at their specified locations. The missing element displays at a random position that doesn’t violate requirements for the positioned elements.
Several elements have before or after set to dash (-)All elements display at the top (or bottom, in case of the after attribute), but the ordering of group of these elements is undefined.
The before or after attribute’s value refers to an element that is not located in the parent node of the element being defined.The element displays at a random location that doesn’t violate requirements for the correctly positioned elements.

<action>

The <action> instruction is deprecated. If the method implementation allows, use the <argument> for <block> or <referenceBlock> to access block public API.

Calls public methods on the block API.

Details: Used to set up the execution of a certain method of the block during block generation; the <action> node must be located in the scope of the <block> node.

Example:

<block class="Magento\Module\Block\Class" name="block">
    <action method="setText">
        <argument name="text" translate="true" xsi:type="string">Text</argument>
    </action>
    <action method="setEnabled">
        <argument name="enabled" xsi:type="boolean">true</argument>
    </action>
</block>

<action> child nodes are translated into block method arguments. Child nodes names are arbitrary. If there are two or more nodes with the same name under <action>, they are passed as one array.

AttributeDescriptionValuesRequired?
methodName of the public method of the block class this tag is located in that is called during block generation.block method nameyes

To pass parameters, use the <argument></argument> instruction.

<referenceBlock> and <referenceContainer>

Updates in <referenceBlock> and <referenceContainer> are applied to the corresponding <block> or <container>.

For example, if you make a reference by <referenceBlock name="right">, you’re targeting the block <block name="right">.

To pass parameters to a block use the <argument></argument> instruction.

AttributeDescriptionValuesRequired?
removeAllows to remove or cancel the removal of the element. When a container is removed, its child elements are removed as well.true/falseno
displayAllows you to disable rendering of specific block or container with all its children (both set directly and by reference). The block’s/container’s and its children’ respective PHP objects are still generated and available for manipulation.true/falseno
    • The remove attribute is optional and its default value is false.
    • This implementation allows you to cancel removal of a block or container in your layout by setting remove attribute value to false. Example:

<referenceBlock name="block.name" remove="true" />
    • The display attribute is optional and its default value is true.
    • You are always able to overwrite this value in your layout. In situation when remove value is true, the display attribute is ignored. Example:

<referenceContainer name="container.name" display="false" />

<move>

Sets the declared block or container element as a child of another element in the specified order.

Example:

<move element="name.of.an.element" destination="name.of.destination.element" as="new_alias" after="name.of.element.after" before="name.of.element.before"/>
  • <move> is skipped if the element to be moved is not defined.
  • If the as attribute is not defined, the current value of the element alias is used. If that is not possible, the value of the name attribute is used instead.
  • During layout generation, the <move> instruction is processed before the removal (set using the remove attribute). This means if any elements are moved to the element scheduled for removal, they will be removed as well.
AttributeDescriptionValuesRequired?
elementName of the element to move.element nameyes
destinationName of the target parent element.element nameyes
asAlias name for the element in the new location.0-9, A-Z, a-z, underscore (_), period (.), dash (-). Case-sensitive.no
after | beforeSpecifies the element’s position relative to siblings. Use dash (-) to position the block before or after all other siblings of its level of nesting. If the attribute is omitted, the element is placed after all siblings.element nameno

<remove>

Is used only to remove the static resources linked in a page <head> section. For removing blocks or containers, use the<remove> attribute for <referenceBlock> and <referenceContainer>. Example of usage:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <head>
        <!-- Remove local resources -->
        <remove src="css/styles-m.css" />
        <remove src="my-js.js"/>
        <remove src="Magento_Catalog::js/compare.js" />
								
	<!-- Remove external resources -->
        <remove src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css"/>
        <remove src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"/>
        <remove src="http://fonts.googleapis.com/css?family=Montserrat" /> 
   </head>

<update>

Includes a certain layout file. Used as follows:

<update handle="{name_of_handle_to_include}"/>

The specified handle is “included” and executed recursively.

<argument>

Used to pass an argument.

AttributeDescriptionValuesRequired?
nameArgument name.uniqueyes
xsi:typeArgument type.string|boolean|object|number|null|arrayyes
translatetrue|falseno

To pass multiple arguments use the following construction:

<arguments>
   <argument></argument>
   <argument></argument>
   ...
</arguments>

To pass an argument that is an array use the following construction:

<argument>
   <item></item>
   <item></item>
   ...
</argument>

Arguments values set in a layout file can be accessed in templates using the get{ArgumentName}() and has{ArgumentName}()methods. The latter returns a boolean defining whether there’s any value set. {ArgumentName} is obtained from the name attribute the following way: for getting the value of <argument name="some_string"> the method name is getSomeString(). Example: Setting a value of css_class in the app/code/Magento/Theme/view/frontend/layout/default.xml layout file:

...
<arguments>
    <argument name="css_class" xsi:type="string">header links</argument>
</arguments>
...

Using the value of css_class in app/code/Magento/Theme/view/frontend/templates/html/title.phtml:

...
$cssClass = $this->getCssClass() ? ' ' . $this->getCssClass() : '';
...
0 Likes
538 Views

You may also like

Leave A Comment

Please enter your name. Please enter an valid email address. Please enter message.