【问题标题】:Fatal error: Call to a member function setTemplate() on a non-object致命错误:在非对象上调用成员函数 setTemplate()
【发布时间】:2013-11-25 04:55:38
【问题描述】:

我在自定义模块中创建了一个块,但它不起作用。我的模块工作正常,但是当我从 footer.phtml 文件调用我的块时,我的块不工作,它显示“致命错误:调用非对象上的成员函数 setTemplate()”。实际上我想使用我的自定义块在我的前端显示一些消息。我在下面提到了我的代码

本地/Monojit/Custom/controllers/IndexController.php

<?php
class Monojit_Custom_IndexController extends Mage_Core_Controller_Front_Action
{
   public function indexAction()
    {   

        $this->loadLayout();    

        $block = $this->getLayout()->createBlock(
            'Mage_Core_Block_Template',
            'my_block_name_here',
            array('template' => 'custom/test.phtml')
        );

        $this->getLayout()->getBlock('content')->append($block);
        $this->renderLayout();
    }

}
?>

本地/Monojit/Custom/Block/Mycustomblock.php

<?php
class Monojit_Custom_Block_Mycustomblock extends Mage_Core_Block_Template

{
//public function _prepareLayout()
//    {
//      return parent::_prepareLayout();
//    }

    public function _construct() {     
        parent::_construct(); 
        $this->setTemplate('custom/test.phtml');     
    }
    public function getmessage()
     {
       $msg = "showing my custom block";       
       return $msg;
     }   
} 
?>

本地/Monojit/自定义/etc/config.xml

<config>

<global>

<modules>

<monojit_custom>

<version>0.1.0</version>

</monojit_custom>

</modules>

<blocks>

<custom>

<rewrite>

<custom>Monojit_Custom_Block_Mycustomblock</custom>

</rewrite>

</custom>

</blocks>
<helpers>
  <custom>
      <class>Monojit_Custom_Helper</class>
  </custom>
</helpers>
</global>

<frontend>

<routers>

<custom>

<use>standard</use>

<args>

<module>Monojit_Custom</module>

<frontName>custom</frontName>

</args>

</custom>

</routers>

<layout>

<updates>

<custom>

<file>custom.xml</file>

</custom>

</updates>

</layout>

</frontend>

</config>

我在 frontend/default/monojit 中创建了一个主题(复制的现代主题)也更改了管理设计配置,在 skin.screenshot pic 中创建了必要的文件夹

设计/前端/默认/monojit/template/custom/test.phtml

//want to fetch data from my block 
<p>This is your custom block called programatically.</p>

localhost/magento/index.php/custom 页面正确显示了我的消息,但是当我从 footer.phtml 页面调用我的块时

<?php echo $this->getLayout()->createBlock('custom/mycustomblock')->setTemplate('custom/test.phtml')->toHtml(); ?>

它显示“致命错误:调用非对象上的成员函数 setTemplate()”我需要创建任何 layout.xml 文件吗?请帮助我如何解决我的问题。谢谢

【问题讨论】:

    标签: magento layout module magento-1.7 block


    【解决方案1】:

    您的块未创建,并且 createBlock 返回一个非对象值。发生这种情况是因为您没有指定自定义命名空间类前缀。您在 XML 配置中的块节点应该看起来像

    <blocks>
        <custom>
            <class>Monojit_Custom_Block</class>
        </custom>
    </blocks>
    

    这意味着 custom 命名空间下的所有内容都将使用 Monojit_Custom_Block 前缀创建,因此 custom/mycustomblock => Monojit_Custom_Block_Mycustomblock。

    但是使用 createBlock 通常是不好的做法,最好使用布局文件:

    【讨论】:

    • 非常感谢。我昨晚试过了,但今天我在看了你的帖子后 5 分钟内就修好了。
    • 你能告诉我如何从我的块页面获取数据到 test.phtml 页面吗?
    • “我的阻止页面”是什么意思?你想获取什么数据? createBlock 方法返回块实例,因此您可以使用它的所有方法。如果您通过 XML 指令创建了块,那么您可以在块模板中使用 $this,或者在父块中使用 $this->getChild('child_name')。
    【解决方案2】:

    尝试:

    在 layout/page.xml 中设置你的块,例如:

    <block type="page/html_footer" name="footer" as="footer" template="page/html/footer.phtml">
        <block type="my_module/block_links" name="my_block_name" as="my_block_name" template="page/template/links.phtml"/>          
    </block> 
    

    在您的 phtml 文件中使用以下代码

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

    【讨论】:

    • 我尝试了这个过程,但不知道为什么它不起作用。在 page.xml 文件中添加下面的代码 并使用 getChildHtml('custom_block') ?> 来自 footer.phtml 页面。
    猜你喜欢
    • 1970-01-01
    • 2012-05-30
    • 2016-08-30
    • 2015-02-06
    • 2012-05-14
    • 2013-11-19
    • 2017-04-30
    • 2015-04-13
    • 2014-10-22
    相关资源
    最近更新 更多