0

the docs read:

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

AS - An alias name that serves as identifier in the scope of the parent element.

They're both unique identifiers. When do I use the name and when the alias?

1 Answer 1

1

Using AS is never required.
You should always add a NAME before adding an AS

AS is an alias to NAME to make it more readable.

It can be used in templates files (.phtml) in getChild / getChildHtml methods, but it can't be used to reference the block in layouts file.

For exemple :

In a first module, you have this layout (simplified):

<page>
    <body>
        <block class="Vendor\Module\Block\Parent" name="vendor_module_parent" template="Vendor_Module::parent.phtml">
            <block class="Vendor\Module\Block\Child" name="vendor_module_child" as="child" template="Vendor_Module::child.phtml">
        </block>
    </body>
</page>

-> In Vendor_Module::parent.phtml :

Both $block->getChildHtml('vendor_module_child') and $block->getChildHtml('child') will work.

-> In another layout :

<page>
    <body>
        <referenceBlock name="vendor_module_child">   <!-- Working -->

        </referenceBlock>
    
        <referenceBlock name="child">   <!-- Not working -->

        </referenceBlock>
    </body>
</page>

Same for move instructions.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.