Understanding $block->getChildHtml()

As I am learning magento, I had this issue from quite a while. Sometimes I was able to use $block->getChildHtml and sometimes not. So I asked help from my colleague Lee and he helped me to understand the whole concept of $block->getChildHtml();
Supposably I want to display this block in one of my view which block of class ‘Title’

echo $block->getChildHtml('sudeep_block');

So in layout file I have to define this as follows

<!-- page title -->
<block class="Vendor\Product\Block\Html\Title" name="page.main.title"
       template="Magento_Catalog::product/view/title.phtml">
    <block class="Vendor\Product\Block\Html\Title" name="product.info.sudeep.block" as="sudeep_block"
           template="Magento_Catalog::product/view/sudeep_block.phtml"/>

</block>


If I don’t put this within <block class=”Vendor\Product\Block\Html\Title”  it will not appear correctly. This is because, we are calling child HTML of this block
Thats the reply from my colleague Lee
“the problem with putting things in the product.info.main container is that its just a container and things are ordered within that container based on the before and after elements that are added in the xml – it just prints all the blocks out that are set.
I’ve moved it into the title block – this sets it as a child, so you can either call ->getChildHtml() – that will pull all child elements in that block
or ->getChildHtml(‘name-of-block’) – and that will call the specific block that you want
if its available obviously!”
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s