0

I am getting same output if I use

referenceContainer

or

referenceBlock

As

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="order_totals">
        <block class="VendorName\ModuleName\Block\Sales\Totals" name="fee_amount" as="fee_amount"/>
    </referenceBlock>
</body>
</page>

And

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="order_totals">
        <block class="VendorName\ModuleName\Block\Sales\Totals" name="fee_amount" as="fee_amount"/>
    </referenceBlock>
</body>
</page>

Layout File is:

VendorName\ModuleName\view\frontend\layout\sales_order_view.xml

I am curious, which is recommended to use. If both can work, then on what specific conditions? I understand basic of container as explained here.

1

Following are major difference between both of them:

Container: Using container we can create a new blank HTML dom using htmlClass and htmlDiv attribute of container. This is to create a blank HTML dom.

ReferenceContainer: There is already a container exist and you want to put your block or container in that container we use referenceContainer tag.

Block: Block works as mediator of our phtml file and the business logic. In block we define the PHP class and the phtml file path and it works to connect both of them. In PHTML file if call the function of block class that is provided in class attribute of tag.

ReferenceBlock: ReferenceBlock is used when we want to use already existing block and want to put our block in that existing block. We use this if we want to add changes to phtml file of that block. In the PHTML file of existing block we use $block->getChild('name_of_child_block') function to render the output of child block.

For more details you can follow documentations:

https://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-instructions.html

If hope this will clear you concept about the block/container and referenceBlock/referenceContainer.

  • Thank you Gaurav for reply, I will have accepted it if it would be to the point. – Ajwad Syed Nov 1 '18 at 13:19
1

It depends on whether "order_totals" was initially created as a block or a container.

So in your case, you'll need to find out how "order_totals" was created.

If you find

<container name="order_totals">

That means you will use "referenceContainer" But if you find

<block name="order_totals">

You'll use "referenceBlock"

  • Thank you Matt, this functionality is implemented in magento vedor folder! I am just rendering it and it is working fine with both referenceBlock and referenecContainer. What is its meaning ? – Ajwad Syed Nov 1 '18 at 13:15
  • Haven't seen a case like this before, but either there are both block and container with the same name, or they are essentially equal depending on what kind of action you're doing with your reference. – Matt Shadowwing Nov 1 '18 at 14:30

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.