1

I have created one block inside my custom module but it's not working. My module is working fine but my block is not working when I call my block from footer.phtml file it shows 'Fatal error: Call to a member function setTemplate() on a non-object'. Actually I want to show some message in my frontend using my custom block.I have mentioned my code below

local/Monojit/Custom/controllers/IndexController.php

<?php
class Monojit_Custom_IndexController extends Mage_Core_Controller_Front_Action
{
   public function indexAction()
    {   

        $this->loadLayout();    

        $block = $this->getLayout()->createBlock(
            'Mage_Core_Block_Template',
            'my_block_name_here',
            array('template' => 'custom/test.phtml')
        );

        $this->getLayout()->getBlock('content')->append($block);
        $this->renderLayout();
    }

}
?>

local/Monojit/Custom/Block/Mycustomblock.php

<?php
class Monojit_Custom_Block_Mycustomblock extends Mage_Core_Block_Template

{
//public function _prepareLayout()
//    {
//      return parent::_prepareLayout();
//    }

    public function _construct() {     
        parent::_construct(); 
        $this->setTemplate('custom/test.phtml');     
    }
    public function getmessage()
     {
       $msg = "showing my custom block";       
       return $msg;
     }   
} 
?>

local/Monojit/Custom/etc/config.xml

<?xml version="1.0"?>

<config>

<global>

<modules>

<monojit_custom>

<version>0.1.0</version>

</monojit_custom>

</modules>

<blocks>

<custom>

<rewrite>

<custom>Monojit_Custom_Block_Mycustomblock</custom>

</rewrite>

</custom>

</blocks>
<helpers>
  <custom>
      <class>Monojit_Custom_Helper</class>
  </custom>
</helpers>
</global>

<frontend>

<routers>

<custom>

<use>standard</use>

<args>

<module>Monojit_Custom</module>

<frontName>custom</frontName>

</args>

</custom>

</routers>

<layout>

<updates>

<custom>

<file>custom.xml</file>

</custom>

</updates>

</layout>

</frontend>

</config>

I have created one theme (copied modern theme) inside frontend/default/monojit Changed admin design configuration as well,created necessary folder as well inside skin.screenshot pic enter image description here

design/frontend/default/monojit/template/custom/test.phtml

//want to fetch data from my block 
<p>This is your custom block called programatically.</p>

localhost/magento/index.php/custom page shows my message correctly but when I call my block from footer.phtml page

<?php echo $this->getLayout()->createBlock('custom/mycustomblock')->setTemplate('custom/test.phtml')->toHtml(); ?>

It shows 'Fatal error: Call to a member function setTemplate() on a non-object' Do I need to create any layout.xml file? Please help me how can i fix my issue.thanks

4

Put the following code under <blocks> node in your config.xml and try.

<global>
    <blocks>
        <custom>
             <class>Monojit_Custom_Block</class>
        </custom>
. . . . 
</global>
|improve this answer|||||
  • good point, I missed that it was not in his config.xml – ProxiBlue Nov 13 '13 at 7:18
  • lot of thanks.your code works like a charm.. – monojit banik Nov 13 '13 at 18:10
  • 1
    Glad to hear that it worked for you :) – Sukeshini Nov 14 '13 at 2:55
2

getLayout() is not a method of the footer block code, hence it is false, not an object.

Creating a child block in this way is not good practice. You should add your block definition in your layouts.

in your case, you would add this in your local.xml layout file:

<reference name="footer">
   <block type="custom/mycustomblock" name="custom.block" as="custom_block" template="custom/test.phtml"/>
</reference>

then in your footer.phtml you can use $this->getChildHtml('custom_block');

if, for whatever reason you do want to continue with placing code in the template, adjust your line to this:

<?php echo Mage::getSingleton('core/layout')->createBlock('custom/mycustomblock')->setTemplate('custom/test.phtml')->toHtml(); ?>
|improve this answer|||||
1
<?php
class My_Booking_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();            
        echo $this->getLayout()->createBlock('core/template')->setTemplate('booking/booking.phtml')->toHtml();

    }
}
|improve this answer|||||

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.