I need to include a block in another block and launch it directly.
What I do now is this code in the first block


then on that newly created block I have this

public function _prepareLayout()
    echo $this->_toHtml();

This works fine, but having a echo in a the block model I don't really feel comfortable.

Is there any proper way to auto-launch this template at call of this block?

== EDIT ==
After trying the solution of @Keyul Shah it doesn't display anything. So here is a more precise code of what I did.

I'm trying to call the block from a Admin Widget Form, so I put this in my XML:

<reference name="widget_instance_edit">
    <block type="myModule/adminhtml_my_block" name="block_name" output="toHtml" template="myModule/toShow.phtml" />

The module is called as everything in _prepareForm() displays, but the phtml is not displayed. If I try to do echo $this->_toHtml(); this time it has an error because it launches it before the parent.

=== FULL CODE ===

class NameSpace_Module_Block_Adminhtml_Module_Images_Edit_Form extends Mage_Adminhtml_Block_Widget_Form
    protected function _prepareForm()
        // I want to call a specific block on top of the Edit Form

        // addFields stuff


class NameSpace_Module_Block_Adminhtml_Module_Images_Tagger extends Mage_Adminhtml_Block_Template
    public function _prepareLayout()
        // This works but is ugly and want to change it in a correct thing
        echo $this->_toHtml();

The tagger.phtml doesn't matter.

How it looks like

=== Revision ===

After much more experience in Magento, I come back to this question and I realize that I was just on the wrong path.
The best way to do it is to create a block extending Mage_Adminhtml_Block_Widget_Form, then simply call $this->setTemplate('path/to/your.phtml'); and then get inspired by the phtml report/grid/container.phtml

  • What do you mean by "launch it directly"? What in the world are you trying to accomplish that native rendering does not handle?? Perhaps adding your template contents would help.
    – benmarks
    Commented Feb 19, 2014 at 13:53
  • Thanks @benmarks I just put the important part of the code so you understand a bit more what I'm doing. It's on a Admin Widget Form.
    – Shadoweb
    Commented Feb 19, 2014 at 16:10
  • From your question a $this->getLayout()->createBlock('module/block')->toHtml(); should work, why is this no proper solution?
    – Flyingmana
    Commented Feb 19, 2014 at 16:18
  • Not really because I will have to do echo $this->getLayout()->createBlock('module/block')->toHtml(); so it will just put the same problem to the other block...
    – Shadoweb
    Commented Feb 19, 2014 at 16:21
  • But all this is just to have a clean code, I can leave it like it is it works, but I hate leaving messy code like this I feel bad...
    – Shadoweb
    Commented Feb 19, 2014 at 16:23

1 Answer 1


The better way is you can use via XML code.

In your Layout.xml file you can defile output attribute. So do not do manually via code.

The below example will show you how to write in xml.

<reference name="header">
       <block type="hssocialmedia/index" name="hssocialmedia_index" output="toHtml"

When your Block called It will automatically return the html format.

  • Thank you for your reply. But I still have a few errors, I edited the original question so you may have a look.
    – Shadoweb
    Commented Feb 19, 2014 at 11:55
  • have you create the myModule/adminhtml_my_block NameSpace_myModule_Block_Adminhtml_my_Block File is created or not?
    – Keyul Shah
    Commented Feb 19, 2014 at 12:02
  • Yes of course otherwise it wouldn't be working on the old version (with the dirty echo).
    – Shadoweb
    Commented Feb 19, 2014 at 12:51
  • This will not work. If you make this instance an output block then it will echo twice.
    – benmarks
    Commented Feb 19, 2014 at 13:54
  • @benmarks If you know answer then please let me know so I can also know new things
    – Keyul Shah
    Commented Feb 19, 2014 at 14:03

Your Answer

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

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