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.

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 boolean – Black May 23 '18 at 11:14
  • did you moved your block in file inside Block directory? – Piyush May 23 '18 at 11:16
  • "Hello.php" is now at "app/code/local/MyExtensions/HelloBlock/Block/Hello.php" – Black May 23 '18 at 11:31
  • everything seems right, please check for typo and clear cache once – Piyush May 23 '18 at 11:44
  • 1
    Yes you were correct... I accidendially named the folders "MyExtension" instead of "MyExtensions". Thanks!! :) – Black May 23 '18 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.