Tuesday, January 21, 2014

Uses of $this->getChildHtml() in Magento


        >> $this->getChildHtml() is a known features to include a child block in a page. I'm simplifying this with an example:
     
      In your XML : 
       <block type="core/template" name="parent_block" template="example/test.phtml">
             <block type="core/template" name="child_block" template="example/test_child.phtml"/>
       </block>

      Now in your test.phtml you can include the child block like : 
             <?php echo $this->getChildHtml('child_block'); ?>

      
       >> You can also pass a variable to this block. I've shown this in my previous post. Now if you want to pass dynamic value to the child block, here's it:

           <?php
                $this->getChild('child_block')->setData('product_id', $productId);
                echo $this->getChildHtml('child_block', false);
            ?>


         Here $productId is your dynamic value and you need to pass "false" (Block Caching No) as  additional parameter in childHtml(). By default it takes "true" (Block Caching Yes).
     
        You can get the product id in test_child.phtml with $this->getProductId()