Thursday 11 February 2016

Magento Load All Child Block In Controller/phtml

Hi All,

    This post will illustrate how to load all child block in the controller or the phtml.
Mostly faced this scenario in the ajax call.

Below we see the sample layout blocks.


    <block type="pricecompar/product" name="product.pricecompar" as="price_comparison" template="pricecompar/price_compar.phtml" >
        <block type="pricecompar/product_price" name="product.pricecompar.price" as="as_price" template="pricecompar/price_compar_price.phtml" />
        
        <block type="pricecompar/product_name" name="product.pricecompar.name" as="as_name" template="pricecompar/price_compar_name.phtml"/>
        
        <block type="pricecompar/product_description" name="product.pricecompar.description" as="as_description" template="pricecompar/price_compar_description.phtml"/>
        
        <block type="pricecompar/product_addtocart" name="product.pricecompar.addtocart" as="as_addtocart" template="pricecompar/price_compar_addtocart.phtml"/>
        
        <action method="addColumn" translate="title">
            <name>description</name>
            <title>Description</title>
            <block>product.pricecompar.description</block>
            <sortorder>30</sortorder>
            <width />
        </action>

        <action method="addColumn" translate="title">
            <name>product_price</name>
            <title>Price</title>
            <block>product.pricecompar.price</block>
            <sortorder>100</sortorder>
            <width>80</width>
        </action>

        <action method="addColumn" translate="title">
            <name>add_to_cart</name>
            <title />
            <block>product.pricecompar.addtocart</block>
            <sortorder>200</sortorder>
            <width>100</width>
        </action>

    </block>
     

In controller/phtml render the above blocks using below code :


<?php

$layout              = Mage::app()->getLayout();

$block_header   = $layout->createBlock('pricecompar/product')->setTemplate('pricecompar/price_compar.phtml');

$block_links1   = $layout->createBlock('pricecompar/product_price','product.pricecompar.price')->setTemplate('pricecompar/price_compar_price.phtml');


/*- Render the first child block -*/

$block_header->setChild('as_price',$block_links1);

$block_links2    = $layout->createBlock('pricecompar/product_name','product.pricecompar.name')->setTemplate('pricecompar/price_compar_name.phtml');


/*- Render the second child block -*/

$block_header->setChild('as_name',$block_links2);

$block_links3    = $layout->createBlock('pricecompar/product_description','product.pricecompar.description')->setTemplate('pricecompar/price_compar_description.phtml');


/*- Render the third child block -*/

$block_header->setChild('as_description',$block_links3);

$block_links4    = $layout->createBlock('pricecompar/product_addtocart','product.pricecompar.addtocart')->setTemplate('pricecompar/price_compar_addtocart.phtml');


/*- Render the fourth child block -*/

$block_header->setChild('as_addtocart',$block_links4);

$block_header->addColumn('description','Description','product.pricecompar.description',30,'');

$block_header->addColumn('product_price','Price','product.pricecompar.price',100,80);
$block_header->addColumn('add_to_cart','','product.pricecompar.addtocart',200,100);

/*- Display the rendered all child blocks in html -*/
echo $block_header->toHtml();


?>


Cheers ...