What's in this topic
Changing layout files is one of the two possible ways to customize page layout in Magento (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.
Common layout instructions
Use the following layout instructions to customize your layout:
<block>
<container>
before
andafter
attributes<action>
<referenceBlock>
and<referenceContainer>
<move>
<update>
<argument>
<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.
Attribute | Description | Values | Required? |
---|---|---|---|
class | Name of a class that implements rendering of a particular block. An object of this class is responsible for actual rendering of block output. | class name | yes |
name | Name 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_n |
0-9, A-Z, a-z, underscore (_), period (.), dash (-). Should start with a letter. Case-sensitive. | no |
before | Used 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 |
after | Used 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 |
template | A template that represents the functionality of the block to which this attribute is assigned. | template file name | no |
as | An 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 |
cacheable | Defines whether a block element is cacheable. This can be used for development purposes and to make needed elements of the page dynamic. | true or false |
no |
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.
Attribute | Description | Values | Required? |
---|---|---|---|
name | A 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 |
label | An arbitrary name to display in the web browser. | any | no |
before | Used 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 |
after | Used 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 |
as | An 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 |
output | Defines 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 obsolete toHtml . Recommended value is 1 . |
no |
htmlTag | Output parameter. If specified, the output is wrapped into specified HTML tag. | Any valid HTML 5 tag. | no |
htmlId | Output 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 |
htmlClass | Output 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 |
...
<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>
...
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.
Attribute | Value | Description |
---|---|---|
before | Dash (-) | The block displays before all other elements in its parent node. |
before | [element name] | The block displays before the named element. |
before | empty value or [element name] is absent | Use the value of after . If that value is empty or absent as well, the element is considered as non-positioned. |
after | Dash (-) | The block displays after all other elements in its parent node. |
after | [element name] | The block displays after the named element. |
after | empty value or [element name] is absent | Use the value of before . If that value is empty or absent as well, the block is considered as non-positioned. |
Examples
Situation | Result |
---|---|
Both before and after attributes are present |
after takes precedence. |
Both before and after attributes are absent or empty |
The 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.
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.
<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.
In the previous example, the value of <arg1>
is passed as the first argument and <arg2>
values are passed as array('one', 'two')
. The list of all available methods depends on the block implementation (for example, the public method of the block class).
Attribute | Description | Values | Required? |
---|---|---|---|
method | Name of the public method of the block class this tag is located in that is called during block generation. | block method name | yes |
<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">
.
<argument></argument>
instruction.
Attribute | Description | Values | Required? |
---|---|---|---|
remove | Allows to remove or cancel the removal of the element. When a container is removed, its child elements are removed as well. | true/false | no |
display | Allows 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/false | no |
- 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" />
display
attribute is optional and its default value is true.<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 thename
attribute is used instead. - During layout generation, the
<move>
instruction is processed before the<remove>
instruction. This means if any elements are moved to the element scheduled for removal, they will be removed as well.
Attribute | Description | Values | Required? |
---|---|---|---|
element | Name of the element to move. | element name | yes |
destination | Name of the target parent element. | element name | yes |
as | Alias name for the element in the new location. | 0-9, A-Z, a-z, underscore (_), period (.), dash (-). Case-sensitive. | no |
after | before | Specifies 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 name | no |
<update>
Includes a certain layout file. Used as follows:<update handle="{name_of_handle_to_include}"/>
<argument>
Used to pass an argument.Attribute | Description | Values | Required? |
---|---|---|---|
name | Argument name. | unique | yes |
xsi:type | Argument type. | string|boolean|object|number|null|array | yes |
translate | true|false | no |
<arguments>
<argument></argument>
<argument></argument>
...
</arguments>
<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>
...
css_class
in app/code/Magento/Theme/view/frontend/templates/html/title.phtml
:
...
$cssClass = $this->getCssClass() ? ' ' . $this->getCssClass() : '';
...
Find us on