Magento 2 nested blocks
Asked Answered
T

2

11

I'm migrating from Magento 1.x to Magento 2

I created default.xml file to customize the existing layout.

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

Now, I know blocks can have templates. So I tried to create a nested block structure (don't know if it is valid 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 the top_header.phtml, I'm trying to call:

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

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

Thurmanthurmann answered 19/12, 2015 at 13:36 Comment(0)
T
11

Silly mistake, I was missing echo:

<?php echo $block->getChildHtml('header.menu'); ?>
Thurmanthurmann answered 19/12, 2015 at 14:4 Comment(0)
A
0

Create a nested containers instead, that should work fine, you can add any classes you want and use any tag for wrapping.

Amarette answered 1/5, 2017 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.