Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between getChildHtml() and getChildChildHtml() in Magento

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.

like image 866
davidselo Avatar asked Jan 18 '12 19:01

davidselo


1 Answers

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);
}
like image 115
Alan Storm Avatar answered Nov 16 '22 00:11

Alan Storm