difference between getChildHtml() and getChildChildHtml() in Magento

11,213

Let's say you're in the root block's phtml template file, and you have a simplified block structure that looks like this

root
    left
        promo_top
        navigation
        promo_bottom
    center
    right

From the root block's template file, to print the left block you'd use getChildHtml.

echo $this->getChildHtml('left');

However, if for some reason you wanted to print the promo_top block in the root template, you'd have to do something like this

$left = $this->getChildBlock('left')
echo $left->getChildHtml('promo_top')

The getChildChildHtml method allows you to do this sort of thing in one simple method call. Again, from the root template

echo $this->getChildChildHtml('left','promo_top');

So, the semantics are

  1. Get My Child Block with the name X
  2. Then, get it's child block with the Y
  3. Render the HTML

If you look at the source code you can see that, ultimately, this method just wraps a call to getChildHtml

#File: app/code/core/Mage/Core/Block/Abstract.php
public function getChildChildHtml($name, $childName = '', $useCache = true, $sorted = false)
{
    if (empty($name)) {
        return '';
    }
    $child = $this->getChild($name);
    if (!$child) {
        return '';
    }
    return $child->getChildHtml($childName, $useCache, $sorted);
}
Share:
11,213

Related videos on Youtube

davidselo
Author by

davidselo

Updated on June 04, 2022

Comments

  • davidselo
    davidselo almost 2 years

    I want to know the differences between this two functions. I understand the behavior of getChildHtml(). It returns the html of the block or all the blocks if you don´t pass any parameters. And I can see

    getChildHtml($name, $useCache, $sorted)
    getChildChildHtml($name, $childName,$useCache, $sorted)
    

    at first sight I a $useCache param that I suposed is to use cache.

  • Ricky Sharma
    Ricky Sharma over 10 years
    Awesome explanation. Thanks Alan.