Skip to content
intermediate Phase 53 · Theme Architecture

Layout XML Deep Dive

Comprehensive guide to Magento 2 Layout XML: handles, blocks, containers, referenceContainer, action, arguments, move, and remove operations

1h
0 problems
Topic Progress 0%

Block and Container Attributes

Block Element Complete Reference

<block class="Magento\Catalog\Block\Product\ListProduct"
       name="product.list"
       template="product/list.phtml"
       as="productList"
       after="-"
       before="-"
       output="1"
       cacheable="1"
       ttl="3600"
       css_class="product-list"
       html_tag="div"
       html_id="product-list-wrapper"
       group_id="">
    <arguments>
        <argument name="title" xsi:type="string">Products</argument>
    </arguments>
</block>

Block Attributes Reference

Attribute Description
class PHP block class
name Unique identifier in layout
template .phtml template file
as Alias for rendering
after/before Sort order
output Auto-render (1=yes)
cacheable Enable caching
ttl Cache time-to-live
css_class CSS class attribute
html_tag Wrapper HTML tag
html_id Wrapper HTML id

Container Element

<container name="content.container"
           htmlTag="div"
           htmlClass="page-main"
           htmlId="maincontent"
           label="Main Content Container"
           output="1">
    <block .../>
</container>

Container Attributes

Attribute Description
htmlTag Wrapper element (div, section, etc.)
htmlClass CSS class for wrapper
htmlId HTML id for wrapper
label Human-readable label
output Auto-render children

ReferenceBlock and ReferenceContainer

ReferenceBlock Operations

<!-- Add child blocks -->
<referenceBlock name="product.info">
    <block class="Vendor\Module\Block\Extra"
           name="extra.info"
           template="extra.phtml"
           after="product.info.price"/>
</referenceBlock>

<!-- Change block class -->
<referenceBlock name="product.info" class="Vendor\Module\Block\CustomProduct"/>

<!-- Change template -->
<referenceBlock name="product.info" template="Vendor_Module::custom.phtml"/>

<!-- Remove block -->
<referenceBlock name="product.info.review" remove="true"/>

<!-- Remove with args -->
<referenceBlock name="product.info.review" remove="true"/>

ReferenceContainer Operations

<!-- Add to container -->
<referenceContainer name="content">
    <container name="custom.section" htmlTag="div" htmlClass="custom">
        <block .../>
    </container>
</referenceContainer>

<!-- Wrap container -->
<referenceContainer name="content" htmlTag="div" htmlClass="content-wrapper"/>

<!-- Remove container -->
<referenceContainer name="sidebar.main" remove="true"/>

Sort Order with after/before

<block name="block.a" .../>
<block name="block.b" after="block.a" .../>
<block name="block.c" before="block.a" .../>
<block name="block.d" after="-" .../>  <!-- Last -->
<block name="block.e" before="-" .../>  <!-- First -->

Action Tag and Method Calls

Action Tag for Method Calls

<block class="Vendor\Module\Block\Custom" name="custom.block">
    <!-- Call method with parameters -->
    <action method="setTitle">
        <argument name="title" xsi:type="string">Custom Title</argument>
    </action>
    
    <!-- Call method with multiple params -->
    <action method="addItem">
        <argument name="type" xsi:type="string">product</argument>
        <argument name="item" xsi:type="array">
            <item name="label" xsi:type="string">Product Name</item>
            <item name="url" xsi:type="string">http://example.com</item>
        </argument>
    </action>
    
    <!-- Call method without arguments -->
    <action method="setCanShowPrice">
        <argument name="canShowPrice" xsi:type="boolean">true</argument>
    </action>
</block>

Common Action Methods

<!-- Set block data -->
<action method="setData">
    <argument name="key" xsi:type="string">value</argument>
</action>

<!-- Add CSS class -->
<action method="addCss">
    <argument name="css" xsi:type="string">Vendor_Module::css/custom.css</argument>
</action>

<!-- Add JS -->
<action method="addJs">
    <argument name="js" xsi:type="string">Vendor_Module::js/custom.js</argument>
</action>

<!-- Set template -->
<action method="setTemplate">
    <argument name="template" xsi:type="string">custom.phtml</argument>
</action>

Action vs Direct Attributes

<!-- Direct attribute (preferred) -->
<block class="..." template="custom.phtml"/>

<!-- Action method (for complex operations) -->
<action method="setTemplate">
    <argument name="template" xsi:type="string">custom.phtml</argument>
</action>

Use direct attributes when possible; action for methods not available as attributes.

Move and Remove Operations

Move Element

<!-- Move block from one container to another -->
<move element="product.info.addto"
      destination="product.info.bottom"
      after="product.info.price"
      before="-"/>

<!-- Move to different parent -->
<move element="newsletter"
      destination="footer"
      after="-"/>

Move Attributes

Attribute Description
element Block/container name to move
destination Target container/block name
after Place after this element
before Place before this element

Remove Element

<!-- Remove a block -->
<referenceBlock name="product.info.review" remove="true"/>

<!-- Remove a container -->
<referenceContainer name="sidebar.main" remove="true"/>

<!-- Conditional remove (with args) -->
<referenceBlock name="product.info" remove="true"/>

Remove vs Hide

<!-- Remove completely from DOM -->
<referenceBlock name="block.name" remove="true"/>

<!-- Hide with CSS (alternative) -->
<referenceBlock name="block.name" cssClass="hidden"/>

Complete Example

<?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>
        <!-- Remove sidebar -->
        <referenceContainer name="sidebar.main" remove="true"/>
        
        <!-- Move addto buttons -->
        <move element="product.info.addto"
              destination="product.info.bottom"
              after="product.info.price"/>
        
        <!-- Add custom block -->
        <referenceContainer name="content">
            <block class="Vendor\Module\Block\Widget"
                   name="custom.widget"
                   template="widget.phtml"/>
        </referenceContainer>
    </body>
</page>

Quiz

1. What does the 'move' element do?

Question 1 options

2. How do you call a block method via layout XML?

Question 2 options

3. What does remove="true" do to a block?

Question 3 options

Flashcards

Question

What is the action tag used for?

Answer

Calling block methods with arguments

Question

How do you move a block?

Answer

<move element="name" destination="target" after="sibling"/>

Question

How do you remove a block?

Answer

<referenceBlock name="name" remove="true"/>

Question

What is the difference between remove and CSS hide?

Answer

Remove deletes from DOM, CSS hide keeps in DOM but invisible

Question

How do you set sort order?

Answer

Use after="elementName" or before="elementName"

Revision Notes

Key Takeaways

  • 1. Block renders content; container groups blocks with HTML wrapper
  • 2. ReferenceBlock/Container modify existing elements
  • 3. Action tag calls block methods with arguments
  • 4. Move element relocates blocks between containers
  • 5. Remove completely deletes elements from DOM

Interview Tips

  • Explain all layout XML elements and their use cases
  • Know when to use action vs direct attributes
  • Discuss move vs remove strategies
  • Be ready to write complex layout configurations

Cheat Sheet

Block: <block class="..." name="..." template="..."/>
Container: <container htmlTag="div" htmlClass="...">
Reference: <referenceBlock name="...">
Action: <action method="methodName"><argument .../></action>
Move: <move element="..." destination="..." after="..."/>
Remove: <referenceBlock name="..." remove="true"/>