I am a magento beginner and need help with creating a new custom block. Basically, I just want the block to show "hello" if it is called.
Module installation xml file, 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>Module configuration xml file, 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>Block class, app/code/local/MyExtensions/HelloBlock/Hello.php.
<?php class MyExtensions_HelloBlock_Block_Hello extends Mage_Core_Block_Template { public function hello() { echo "hello"; } }Template file for the block, 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"
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
I was following this tutorial.
1 Answer 1
You have added wrong block class, it should be app/code/local/MyExtensions/HelloBlock/Block/Hello.php instead of app/code/local/MyExtensions/HelloBlock/Hello.php
You are missing Block directory in the module
-
I still get
Fatal error: Call to a member function setTemplate() on booleanBlack– Black2018年05月23日 11:14:22 +00:00Commented May 23, 2018 at 11:14 -
did you moved your block in file inside
Blockdirectory?Piyush– Piyush2018年05月23日 11:16:28 +00:00Commented May 23, 2018 at 11:16 -
"Hello.php" is now at "app/code/local/MyExtensions/HelloBlock/Block/Hello.php"Black– Black2018年05月23日 11:31:40 +00:00Commented May 23, 2018 at 11:31
-
everything seems right, please check for typo and clear cache oncePiyush– Piyush2018年05月23日 11:44:13 +00:00Commented May 23, 2018 at 11:44
-
1Yes you were correct... I accidendially named the folders "MyExtension" instead of "MyExtensions". Thanks!! :)Black– Black2018年05月23日 12:45:44 +00:00Commented May 23, 2018 at 12:45