3

I was trying to do/learn something and i am at topmenu.phtml file , i have seen $block->getchildhtml() there and when i go to block which is namespace Magento\Theme\Block\Html; with the name topmenu.php , i am not seeing any getchildhtml function there . I did research on what getchildhtml function is i have realized it is comming from the layout but when i go to layout which is default.xml file in the module-theme folder there is

<block class="Magento\Framework\View\Element\Template" name="store.menu" group="navigation-sections" template="Magento_Theme::html/container.phtml">
                <arguments>
                    <argument name="title" translate="true" xsi:type="string">Menu</argument>
                </arguments>
                <block class="Magento\Theme\Block\Html\Topmenu" name="catalog.topnav" template="Magento_Theme::html/topmenu.phtml" ttl="3600" before="-"/>
</block>

there is no child of this block so from where this getchildhtml will see and print result ?

1

$block->getChildHtml()
will return child block html. So in your case create child block using xml, this code snippet will automatically return html of that child block. Ex.


<block class="Magento\Framework\View\Element\Template" name="store.menu" group="navigation-sections" template="Magento_Theme::html/container.phtml">
    <arguments>
        <argument name="title" translate="true" xsi:type="string">Menu</argument>
    </arguments>
    <block class="Magento\Theme\Block\Html\Topmenu" name="catalog.topnav" template="Magento_Theme::html/topmenu.phtml" ttl="3600" before="-">
        <block name="sample.testblock" template="Vendor_Module::sample.phtml"/>
    </block>
</block>

<block name="sample.testblock" template="Vendor_Module::sample.phtml"/> block will render automatically.

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.