卡在no_frills_magento_layout第2.4节上



我一直在关注本指南这是我正在使用的代码:

public function complexAction()
        {
            $layout= Mage::getSingleton('core/layout');
            $path = Mage::getModuleDir('', 'Nofrills_Booklayout') . DS . 'page-layouts' . DS . 'complex.xml';
            $xml = simplexml_load_file($path, Mage::getConfig()->getModelClassName('core/layout_element'));
            $layout->setXml($xml);
            $layout->generateBlocks();
            echo $layout->setDirectOutput(true)->getOutput();
        }                   
    }

加载相应的URL后,我得到的就是白屏。我var_dump $路径和$ xml,似乎都显示了正确的信息。但是当我对以下情况做同样的事情时:

$layout->setDirectOutput(true)->getOutput();

我得到:

string(0) ""

任何建议都会有所帮助。

complex.xml的原始代码

<layout>    
    <block type="nofrills_booklayout/template" name="root" template="simple-page/2col.phtml" output="toHtml">
        <block type="nofrills_booklayout/template" name="additional_head" template="simple-page/head.phtml" />
        <block type="nofrills_booklayout/template" name="sidebar">
            <action method="setTemplate"><template>simple-page/sidebar.phtml</template></action>
        </block>
        <block type="core/text_list" name="content" />
    </block>    
</layout>

好的,因此,基于上面的评论,听起来像getOutput返回一个空字符串,而空白的白色屏幕不是隐藏的PHP错误,这是Magento/PHP的结果没有为布局提供任何内容。

第一个调试步骤:确保您正在加载您认为自己的complex.xml。以下应回应文件的内容

header('Content-Type: text/plain'); //tell the browser not to render HTML
echo $xml->asXml();
echo "nDonen";

假设您看到了文件中相同的XML,请继续进行下一步。

以下布局XML(在complex.xml中)

<layout>    
    <block type="nofrills_booklayout/template" name="root" template="simple-page/2col.phtml" output="toHtml">
        <block type="nofrills_booklayout/template" name="additional_head" template="simple-page/head.phtml" />
        <block type="nofrills_booklayout/template" name="sidebar">
            <action method="setTemplate"><template>simple-page/sidebar.phtml</template></action>
        </block>
        <block type="core/text_list" name="content" />
    </block>    
</layout>

应该

  • 实例化nofrills_booklayout/template
  • 将该块的模板设置为simple-page/2col.phtml
  • 渲染该模板simple-page/2col.phtml

simple-page/2col.phtml模板包含以下

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
    <?php echo $this->getChildhtml('additional_head'); ?>
</head>
<body>
    <?php echo $this->getChildhtml('sidebar'); ?>
    <section>
    <?php echo $this->getChildhtml('content'); ?>
    </section>
</body>
</html>

因此,由于您的代码呈现一个空字符串与带有HTML骨架的空白页,所以告诉我Magento/PHP永远不会到达2col.phtml。我会

  • 确保"允许符号链接"在后端设置为"是"。尽管有标签,但此功能实际上意味着"不要让人们在app/design之外渲染PHTML文件,而无褶边模块会有些混乱,因此您无需强调app/design,直到准备就绪。在书中走了很远,所以我认为这不是问题,但是值得检查

  • 确保您当前的模块代码具有simple-page/2col.phtml

  • 尝试直接在PHP中创建模板块 - 此功能是否可行?

$block = Mage::getSingleton('core/layout')
    ->createBlock('nofrills_booklayout/template')
    ->setTemplate('simple-page/2col.phtml');
var_dump($block->toHtml());

我的猜测是您的simple-page/2col.phtml缺失,或者出于某些原因无法加载PHP。希望上述使您走上正确的轨道。如果您需要额外的帮助,请随时通过支持取得联系。

最新更新