当前位置: 动力学知识库 > 问答 > 编程问答 >

No Frills Magento Layout error while trying to create block

问题描述:

I have been following the No Frills Magento Layout book. All examples were working fine until this section:

http://localhost/magento/nofrills_booklayout/index/layout

local/Nofrills/Booklayout/etc/config.xml:

app/code/local/Nofrills/Booklayout/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>

<modules>

<Nofrills_Booklayout>

<version>0.1.0</version>

</Nofrills_Booklayout>

</modules>

<frontend>

<routers>

<nofrills_booklayout>

<use>standard</use>

<args>

<module>Nofrills_Booklayout</module>

<frontName>nofrills_booklayout</frontName>

</args>

</nofrills_booklayout>

</routers>

</frontend>

<global>

<blocks>

<nofrills_booklayout>

<class>Nofrills_Booklayout_Block</class>

</nofrills_booklayout>

</blocks>

<models>

<nofrills_booklayout>

<class>Nofrills_Booklayout_Model</class>

</nofrills_booklayout>

</models>

<helpers>

<nofrills_booklayout>

<class>Nofrills_Booklayout_Helper</class>

</nofrills_booklayout>

</helpers>

</global>

</config>

local/Nofrills/BookLayout/Block/HelloWorld.php:

<?php

class Nofrills_Booklayout_Block_HelloWorld extends Mage_Core_Block_Template

{

public function _construct()

{

$this->setTemplate('helloworld.phtml');

return parent::_construct();

}

public function _beforeToHtml()

{

$block_1 = new Mage_Core_Block_Text();

$block_1->setText('The first sentence. ');

$this->setChild('the_first', $block_1);

$block_2 = new Mage_Core_Block_Text();

$block_2->setText('The second sentence. ');

$this->setChild('the_second', $block_2);

}

public function fetchTitle()

{

return 'Hello Fancy World';

}

}

local/Nofrills/Booklayout/controllers/IndexController.php:

public function layoutAction()

{

$layout = Mage::getSingleton('core/layout');

$block = $this->getLayout()->createBlock('nofrills_booklayout/helloworld');

echo $block->toHtml();

}

And that gives me:

Call to a member function toHtml() on a non-object in...

网友答案:

try this one:

public function layoutAction()
{
    $this->loadLayout();
    $this->_addContent($this->getLayout()->createBlock('nofrills_booklayout/helloworld'));
    $this->renderLayout();
}
网友答案:

As Marius said in the comments section, changing "helloworld" to "helloWorld" make it happen.

分享给朋友:
您可能感兴趣的文章:
随机阅读: