Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top