Magento中getChildHtml()和getChildChildHtml()之间的区别

dav*_*elo 17 templates magento

我想知道这两个功能之间的区别.我理解getChildHtml()的行为.如果你没有传递任何参数,它会返回块的html或所有块.我可以看到

getChildHtml($name, $useCache, $sorted)
getChildChildHtml($name, $childName,$useCache, $sorted)
Run Code Online (Sandbox Code Playgroud)

乍一看,我使用的$ useCache参数是使用缓存.

Ala*_*orm 43

假设您在根块的phtml模板文件中,并且您有一个简化的块结构,如下所示

root
    left
        promo_top
        navigation
        promo_bottom
    center
    right
Run Code Online (Sandbox Code Playgroud)

从根块的模板文件中,打印您要使用的左侧块getChildHtml.

echo $this->getChildHtml('left');
Run Code Online (Sandbox Code Playgroud)

但是,如果由于某种原因你想在根模板中打印promo_top块,你必须做这样的事情

$left = $this->getChildBlock('left')
echo $left->getChildHtml('promo_top')
Run Code Online (Sandbox Code Playgroud)

getChildChildHtml方法允许您在一个简单的方法调用中执行此类操作.再次,从根模板

echo $this->getChildChildHtml('left','promo_top');
Run Code Online (Sandbox Code Playgroud)

所以,语义是

  1. 获取名为X的My Child Block
  2. 然后,用Y来获取它的子块
  3. 渲染HTML

如果你查看源代码,你可以看到,最终,这个方法只包含一个调用 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);
}
Run Code Online (Sandbox Code Playgroud)