Magento 2 Blocks – How to Load All Child Blocks in Controller

ajaxblockslayoutrender

I am using Ajax to add to cart, so need to update the cart side bar.

I have try this code in controller to render the block.

Mage::app()->getLayout()->getBlockSingleton('page/html')->setTemplate("page/html/upper.phtml")->toHtml();

but it render the parent block and doesn't render the child blocks.

And layout file is

   <block type="core/text_list" name="upper" as="upper" template="page/html/upper.phtml">
       <block type="checkout/cart" name="top.cart" as="cart_overview">
            <action method="setCartTemplate"><value>checkout/topcart.phtml</value></action>
            <action method="setEmptyTemplate"><value>checkout/cart/cartEmpty.phtml</value></action>
            <action method="chooseTemplate"/>
            <action method="addItemRender"><type>simple</type><block>checkout/cart_item_renderer</block><template>checkout/cart/sidebar/default.phtml</template></action>
            <action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>checkout/cart/sidebar/default.phtml</template></action>
            <action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>checkout/cart/sidebar/default.phtml</template></action>
       </block>
   </block>

how can i render child block in controller.

Assist me.

Best Answer

Please try this code:-

$layout         = Mage::app()->getLayout();
 $block_header   = $layout->createBlock('block/block')->setTemplate('template_url');

 $block_links1  = $layout->createBlock('block/block','block_name')->setTemplate('template_url');
 $block_header->setChild('block_as',$block_links1);

 $block_links2  = $layout->createBlock('block/block','block_name')->setTemplate('template_url');
 $block_header->setChild('block_as',$block_links2);

 $block_links    = $layout->createBlock('block/block','block_name')->setTemplate('template_url');
 $block_header->setChild('block_as',$block_links);

 $block_links->addItemRender('configurable','checkout/cart_item_renderer_configurable','checkout/cart/sidebar/default.phtml');
 $slide = $block_header->toHtml();
Related Topic