坚持No

编程入门 行业动态 更新时间:2024-10-09 16:26:48
坚持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(); } }

加载相应的网址后,我得到的是一个白色的屏幕。 我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>

I have been following this guide fairly successfully up to this point here is the code I am working with:

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(); } }

After loading the corresponding url all I get is a white screen. I var_dumped $path and $Xml and both seem to display the right info. but when I do the same with:

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

I get:

string(0) ""

Any advice will be helpful.

original code from 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 "\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 。 我会

确保后端中的“允许符号链接”设置为“是”。 尽管有它的标签,但这个功能实际上意味着“不要让人们在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无法加载它。 希望上面的内容让您走上正轨。 如果您需要其他帮助,请随时通过支持与我们联系 。

OK, so based on your comments above, it sounds like getOutput returns an empty string, and that the blank white screen isn't a hidden PHP error, it's the result of Magento/PHP not rendering anything for the layout.

First debugging step: Make sure you're loading the complex.xml you think you are. The following should echo back the contents of the file

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

Assuming you see the same XML that's in the file, move on to the next steps.

The following layout XML (in 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>

should

Instantiate a nofrills_booklayout/template block Set that block's template to simple-page/2col.phtml Render that template simple-page/2col.phtml

The simple-page/2col.phtml template contains the following

<!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>

So, since your code renders an empty string vs. a blank page with an HTML skeleton, that tells me Magento/PHP never get to 2col.phtml. I would

Make sure the "Allow Symlinks" is set to yes in the backend. Despite its label, this feature actually means "don't let people render phtml files outside of app/design, and the No Frills modules mess with this a little so you don't need to stress about app/design until you're ready. You've gotten this far in the book, so I don't think that's a problem, but it's worth checking

Make sure your current module code has a simple-page/2col.phtml

Try creating a template block directly in PHP -- does this work?

.

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

My guess is your simple-page/2col.phtml is missing, or PHP can't load it for some reasons. Hopefully the above gets you on the right track. Feel free to get in touch via support if you need additional assistance.

更多推荐

本文发布于:2023-08-07 12:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464309.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!