Magento Header – Add Custom Module HTML in Magento 1.7 and 1.8

headermagento-1.7magento-1.8

I try below code is working for add a phtml file in header of Magento.

 <reference name="header">
    <block type="your_module/yourblockclass" name="yourblockname" template="your_module/yourtemplate.phtml"/>
 </reference>

Add the code above to the -layou-handle in your custom layout.xml file. Fetch the block via.

$this->getChildHtml()

from within the header.phtml file.This way it's working fine.

But Suppose you have some custom theme.

you install module and it will create the header.phtml file in app/design/frontend/default/default/page/html/header.phtml in this folder, so the custom theme header.phtml file called this file is not called.

so how to add any phtml file in header for any theme with out use of $this->getChildHtml().

Best Answer

You can add the block by adding output="toHtml" to its declaration. But as long as header block is not a structural one (not of the core/text_list type) you can't really control the positioning of your block inside of the header.

The problem here is that custom module which overrides generic header.phtml which is not good practice. I'd suggest refactoring the module at least moving the modifications which have to me made to header.phtml into your custom theme.

Update:

As I see in your extension there is absolutely no matter where to add the block as you will position it absolutely in any way. So you can try the following:

<reference name="header">
   <block type="your_module/your_block_class" name="your_block_name" template="your_module/your_template.phtml" output="toHtml"/>
</reference>