0

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.

  1. 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>
    
  2. 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>
    
  3. 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";
     }
    }
    
  4. 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.

Murtuza Zabuawala
14.8k10 gold badges47 silver badges76 bronze badges
asked May 23, 2018 at 7:59

1 Answer 1

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

answered May 23, 2018 at 8:06
5
  • I still get Fatal error: Call to a member function setTemplate() on boolean Commented May 23, 2018 at 11:14
  • did you moved your block in file inside Block directory? Commented May 23, 2018 at 11:16
  • "Hello.php" is now at "app/code/local/MyExtensions/HelloBlock/Block/Hello.php" Commented May 23, 2018 at 11:31
  • everything seems right, please check for typo and clear cache once Commented May 23, 2018 at 11:44
  • 1
    Yes you were correct... I accidendially named the folders "MyExtension" instead of "MyExtensions". Thanks!! :) Commented May 23, 2018 at 12:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.