Magento 2 nested blocks

I am moving from Magento 1.x to Magento 2

I created a default.xml file to customize an existing layout.

My situation is that I have a div named "main-header", and I want a nested div named "menu" to be entered inside it.

Now I know that there can be patterns in blocks. So I tried to create a structure of nested blocks (I don’t know if this is really in Magento 2):

<referenceContainer name="header.container"> <container name="common-header" label="Header common to all pages" as="common-header" htmlTag="div" htmlClass="main-header"> <block class="Mymodule\Test\Block\Header" template="header/top_header.phtml"> <block name="header.menu" class="Mymodule\Test\Block\Menu" template="header/menu.phtml"/> </block> </container> </referenceContainer> 

In top_header.phtml, I am trying to call:

 <?php $block->getChildHtml('header.menu'); ?> 

I know the above statement that I took from Magento 1.x

+5
source share
2 answers

Stupid mistake, I missed the echo:

 <?php echo $block->getChildHtml('header.menu'); ?> 
+3
source

Create nested containers instead, this should work fine, you can add any classes you want and use any tag for packaging.

0
source

Source: https://habr.com/ru/post/1238711/


All Articles