10

I've been doing some work in Magento 2 and have been overwriting some modules i've installed ( on the frontend ).

However today i've just come across something that I've not encountered before. The module I've been using has a block inside of it which has the following:

<referenceBlock name="product.info.details">
    <block class="Vender\Module\Block\Catalog\Related" name="posts.tab" as="blog.posts"
               template="Vender_Module::catalog/posts.phtml" group="detailed_info">
    </block>
</referenceBlock>

It sets its name:

posts.tab

and then uses the as to allow it to be called via:

as="blog.posts"

When i tried to reference the block to be removed using the as name I noticed that it wasn't removing it from the page:

<referenceBlock name="product.info.details">
    <referenceBlock as="blog.posts" remove="true"/>
</referenceBlock>

so i went to see what the page actually called in on the frontend ( just incase it was being renamed somewhere else or wasn't using the posts.tab name ) - the block name is being called in with posts.tab , which is fine as i can just reference that block to remove it. That worked fine.

But my question is, why isn't the block being pulled through using as and instead being pulled through using the name.

Secondly what is the point in the as, because obviously blocks can't have conflicting names anyway and should be unique - but i might be totally wrong.

2

In this instance it seems like the as attribute is useless (unless it's used elsewhere), I don't think it makes a difference if it's called via name or the alias so it's up to the developer to choose what to use - just note that if an alias is set you will need to use that when calling getChildHtml.

Personally I'll only use alias to shorten a long block name and/or to make it easily readable when rendering it via a template. For example if checkout.header.logo.text needs to be used as logo.text and header.logo.text are already used, you could then use the alias logoText on all the blocks. This wouldn't affect the rendering of the other blocks using the same alias unless they are set in the same layout handle and reference.

TL:DR It doesn't make much of a difference, it's more for readability/shortening names.

Notes

This M1 question has a bit more info to - When to use 'name' and when to use 'as'

I'm not sure if this applies to M2 but in M1 there was at least one argument called alias which used the as attribute, I think it was related to inserting or unsetting blocks.

According to Fabian Schmengler: "If the block was created without defining an alias, the alias defaults to the name." That is referring to M1 but I presume the same thing applies to M2.

  • Just now on 2.3.1 I couldn't getChildHtml by name because the block had "as" alias. Only after deleting the alias I could get the block in a template. – TheKitMurkit Jun 11 at 16:04
  • Good point @TheKitMurkit - I've added a bit to my answer that states "if an alias is set you will need to use that when calling getChildHtml.". Thanks. – Ben Crook Jun 12 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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