简体   繁体   English

创建一个新块并将其添加到模板

[英]Create a new block and add it to a template

I am a Magento beginner and need help with creating a new custom block. 我是Magento初学者,在创建新的自定义块时需要帮助。 Basically I just want the block to show "hello" if it is called. 基本上,我只希望该块在被调用时显示“ hello”。

  1. Module installation xml file, app/etc/modules/MyExtensions_HelloBlock.xml 模块安装xml文件, app / etc / modules / MyExtensions_HelloBlock.xml

     <?xml version="1.0" encoding="UTF-8"?> <config> <modules> <MyExtensions_HelloBlock> <active>true</active> <codePool>local</codePool> </MyExtensions_HelloBlock> </modules> </config> 
  2. Module configuration xml file, app/code/local/MyExtensions/HelloBlock/etc/config.xml 模块配置xml文件, app / code / local / MyExtensions / HelloBlock / etc / config.xml

     <?xml version="1.0" encoding="UTF-8"?> <config> <modules> <MyExtensions_HelloBlock> <version>0.0.1</version> </MyExtensions_HelloBlock> </modules> <global> <blocks> <helloblock> <class>MyExtensions_HelloBlock_Block</class> </helloblock> </blocks> </global> </config> 
  3. Block class, app/code/local/MyExtensions/HelloBlock/Hello.php 块类, 应用程序/代码/本地/MyExtensions/HelloBlock/Hello.php

     <?php class MyExtensions_HelloBlock_Block_Hello extends Mage_Core_Block_Template { public function hello() { echo "hello"; } } ?> 
  4. Template file for the block, app/design/frontend/default/default/template/helloblock/hello.phtml 块的模板文件, app / design / frontend / default / default / template / helloblock / hello.phtml

     <?php $this->hello(); ?> 

Then I call my new block like this in the template "app/design/frontend/venedor/default/template/page/1column.phtml": 然后在模板“ app / design / frontend / venedor / default / template / page / 1column.phtml”中将这样的新块称为:

echo $this->getLayout()->createBlock('helloblock/hello')->setTemplate('helloblock/hello.phtml')->toHtml();

Result: 结果:

Fatal error: Call to a member function setTemplate() on boolean in /app/design/frontend/venedor/default/template/page/1column.phtml on line 58 致命错误:在第58行的/app/design/frontend/venedor/default/template/page/1column.phtml中的布尔值上调用成员函数setTemplate()

I was following this tutorial . 我正在关注本教程

Block folder is missing 文件夹丢失

Your block's path should be app/code/local/MyExtensions/HelloBlock/Block/Hello.php 您区块的路径应为app / code / local / MyExtensions / HelloBlock / Block / Hello.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM