Magento:page / html_wrapper块为空

时间:2012-02-20 09:06:37

标签: magento

Page.xml的一小部分:

<layout version="0.1.0">
     ....
      <default translate="label" module="page">
        <label>All Pages</label>
        <block type="page/html" name="root" output="toHtml" template="page/2columns-left.phtml">
             <block type="page/html_header" name="header" as="header" /> <!-- work -->
             <block type="page/html_wrapper" name="u.Top.Menu" as="u_Top_Menu" translate="label"> <!-- doesn't work -->
                    <label>top menu</label>
                    <action method="setElementTagName"><value>div</value></action>
                    <action method="setElementClass"><value>sub-menu</value></action>
             </block>
         ...
         </block>
....

在2columns-left.phtml中输出:

<?php echo $this->getChildHtml('u_Top_Menu'); ?>

但它总是回报空值。我有点困惑。我在这里做错了什么?

3 个答案:

答案 0 :(得分:4)

当您忘记将子块添加到html包装器块时会发生这种情况。看看这个块类源代码:

class Mage_Page_Block_Html_Wrapper extends Mage_Core_Block_Abstract
{
    /**
     * Whether block should render its content if there are no children (no)
     * @var bool
     */
    protected $_dependsOnChildren = true;

    /**
     * Render the wrapper element html
     * Supports different optional parameters, set in data by keys:
     * - element_tag_name (div by default)
     * - element_id
     * - element_class
     * - element_other_attributes
     *
     * Renders all children inside the element.
     *
     * @return string
     */
    protected function _toHtml()
    {
        $html = empty($this->_children) ? '' : trim($this->getChildHtml('', true, true));
        if ($this->_dependsOnChildren && empty($html)) {
            return '';
        }

...

答案 1 :(得分:1)

据我所知,你做的一切都是正确的。通常会引起人们兴趣的是as vs name问题,其中一个块通过它name在xml中引用,但在模板中通过它as引用。您似乎没有陷入此陷阱,所以我最好的猜测是您的缓存尚未清除。 rm -rf var/cache/mage-*应该有希望解决这个问题。

答案 2 :(得分:0)

Zyava是正确的,但我有一个解决方法!

如果您不想将子项添加到块中,但仍需要它进行渲染,则有一个类函数(dependsOnChildren)允许您从布局XML中设置此_dependsOnChildren标志这样:

<block type="page/html_wrapper" name="u.Top.Menu" as="u_Top_Menu" translate="label"> <!-- doesn't work -->
    <label>top menu</label>
    <action method="setElementTagName"><value>div</value></action>
    <action method="setElementClass"><value>sub-menu</value></action>
    <!-- This will tell PhP to call $blockClass->dependsOnChildren(0); before rendering. -->
    <action method="dependsOnChildren"><value>0</value></action>
</block>

这是该功能(供参考)

app / code / core / Mage / Page / Block / Html / Wrapper.php @ Line 80

/**
 * Setter whether this block depends on children
 * @param $depends
 * @return Mage_Page_Block_Html_Wrapper
 */
public function dependsOnChildren($depends = '0')
{
    $this->_dependsOnChildren = (bool)(int)$depends;
    return $this;
}