Magento:page/html\u包装块为空

Magento:page/html\u包装块为空,magento,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.pht

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>
....
并在2列s-left.phtml中输出它:

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

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

据我所知,你做的一切都是正确的。通常让人感到困惑的是as-vs-name问题,即块在xml中通过它的名称来引用,但在模板中通过它的as来引用。你似乎没有掉进这个陷阱,所以我最好的猜测是你的缓存没有被清理。rm-rf var/cache/mage-*有望解决这个问题。

据我所知,您所做的一切都是正确的。通常让人感到困惑的是as-vs-name问题,即块在xml中通过它的名称来引用,但在模板中通过它的as来引用。你似乎没有掉进这个陷阱,所以我最好的猜测是你的缓存没有被清理。rm-rf var/cache/mage-*有望解决这个问题。

这就是当您忘记将子块添加到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 '';
        }

这是当您忘记将子块添加到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 '';
        }

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>

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>

如果您尝试$this->getChildHtml'u.Top.Menu';,会怎么样;?我同意缓存可能是个问题,我试过了。不幸的是,这没用。你把getChildHtml'u_Top_菜单'放在哪里?布局xml和phtml模板文件的路径是什么?还有一件事-2-columns-left.phtml可能会在其他地方重写。所以试着把这个echo调用放到其他一些文件中。是的,我做了echo,甚至我为$this->getChildHtml'u_Top_Menu'打印了var_dump。它得到的是空字符串。如果您尝试$this->getChildHtml'u.Top.Menu';,会怎么样;?我同意缓存可能是个问题,我试过了。不幸的是,这没用。你把getChildHtml'u_Top_菜单'放在哪里?布局xml和phtml模板文件的路径是什么?还有一件事-2-columns-left.phtml可能会在其他地方重写。所以试着把这个echo调用放到其他一些文件中。是的,我做了echo,甚至我为$this->getChildHtml'u_Top_Menu'打印了var_dump。它得到空字符串。我关闭了缓存,并试图手动清除它。它仍然不工作。我关闭了缓存,并试图手动清除它。它仍然不起作用。