TransWikia.com

Magento 2 - cannot insert block inside another block from the core

Magento Asked on March 3, 2021

im trying to insert a custom block inside another block from the core. but for some reasons i cannot do it using referenceBlock. though its working if i use referenceContainer on the ‘content’, im trying to do it in checkout_cart_index.xml. heres my layout.xml.

<body>
    //this one is working
    <referenceContainer name="content">
        <block class="MagentoFrameworkViewElementTemplate"
               name="sample.name"
               before="-"
               template="My_Module::template.phtml" />
    </referenceContainer>

    //this one is not working. 
    //i also tried using renderer.list as reference block name. still not working
    <referenceBlock name="checkout.cart.item.renderers">
        <block class="MagentoFrameworkViewElementTemplate"
               name="another.sample.name"
              template="My_Module::template.phtml"/>
    </referenceBlock>

</body>

i have no idea whats wrong since the referenceContainer is working

One Answer

That is one of the main differences between container and block.

Container will automatically render it's child blocks whereas block will not.

To render a child inside most blocks you must add <?= $block->getChildHtml('another.sample.name') ?> inside the parent blocks template. You need to pass in the alias defined on the block, if no alias is set then it will use the name attribute.

I say most blocks as some blocks will contain <?= $block->getChildHtml() ?> which will render all child blocks similar to a container.

Answered by Ben Crook on March 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP