停留在 No_Frills_Magento_layout 第 2.4 节

Stuck on No_Frills_Magento_layout section 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_dumped $path 和 $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 returns 是一个空字符串,空白屏幕不是隐藏的 PHP 错误,它是Magento/PHP 的结果没有为布局呈现任何内容。

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

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

假设您在文件中看到相同的 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。我会

  • 确保 "Allow Symlinks" 在后端设置为 yes。尽管有标签,但此功能实际上意味着“不要让人们在 app/design 之外渲染 phtml 文件,而 No Frills 模块会对此造成一点混乱,因此您无需强调 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 由于某些原因无法加载它。希望上面的内容能让你走上正确的轨道。如果您需要其他帮助,请随时 get in touch via support