2

In Magento 1 we have the possibility the add a block in a root block so it will not be displayed because root is not a core/text_list block type (structural block), it doesn't render automatically the content block without calling it in phtml like : getChildHtml(), getBlockHtml(). So we need to call it in phtml to render it.

Example:

local.xml

<catalog_product_view>
   <reference name="root">
      <block type="core/template" name="block.name" template="page/html/file1.phtml"/>
   </reference>
</catalog_product_view>

Here we need to call that block to render it like this:

<?php echo $this->getBlockHtml('block.name'); ?>

But in Magento 2 the root container render automatically the root block why ?

I need like Magento 1, declare my block in xml, this xml doesn't render it, and it's me who must call it where i want.

I don't know if there is an equivalent of root in Magento 2

Example:

<referenceContainer name="root">
    <block class="Magento\Framework\View\Element\Template" name="block.name" template="Magento_Theme::page/html/file1.phtml" />
</referenceContainer>
1

You can pass your block without <referenceContainer> in your layout file (i.e catalog_product_view, checkout_cart_index) under <body> tag then you can call in your .phtml file with echo $block->getBlockHtml('some.name');

  • So without a container, the block is displayed in root right ? And do you know if it is possible to declare a layout handle in default.xml like M1, in local.xml we declare a layout handle then we include our block that it will be displayed in that layout handle, it's really useful I don't know why they removed it in M2 ! – PЯINCƏ Apr 16 '18 at 8:07
  • If you want to put some block on all page(i.e footer link, top link) then you can put in default.xml @Prince – Keyur Shah Apr 16 '18 at 8:28
  • Yes I know this but in Magento 2 we are obliged to add each block in each layout handle xml, we can't put all changes in one xml like local.xml in Magento1 – PЯINCƏ Apr 16 '18 at 8:49

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.