Magento – Cannot Customize Header Block by Custom Module

header

First, I can easily change header template by

<reference name="header">
      <action method="setTemplate"><template>customizer/header.phtml</template></action>
</reference>

But, I want to add some functions to header block, so I try to extend:

class My_Customizer_Block_Header extends Mage_Page_Block_Html_Header

So I change my custom layout xml to

<reference name="header">
      <block type="customizer/header" name="header" as="header" template="customizer/header.phtml" />
</reference>

Now, $this->getTemplate() returns null, ($this->hasData('template') returns null as well)

Even if I change xml to

<reference name="header">
      <block type="customizer/header" name="header" as="header">
             <action method="setTemplate"><template>customizer/header.phtml</template></action>
      </block>
</reference>

it still doesn't work.

I also try to extend Mage_Core_Block_Template but it doesn't work.

I don't know why I cannot set custom template for header by custom module.
Someone asked here but the answer is not clear enough to me.

Note:

  • I don't want to copy Mage/Page/Block/Html/Header.php to local directory.

  • I don't want to rewite completely header class by config.xml, just change header in my custom handle

Thank you.

UPDATE:

For clearer,

  1. I'm using a custom theme base on rwd package (1.9.0.1)

  2. My needed is:

    • All pages still use default header of custome theme (rwd/mytheme/page/html/header.phtml)
    • Custom header for ONLY custom handle (eg: domain.com/customizer)
    • I nead to use completely different header for my custom handle, that's why I try to reuse header name. (I also intent to remove each default header child, then add my custom child for header, but I think it's not best way)

Best Answer

When you want to customize header block, there are different approach to do that. I will point out some of the ways that you can rely on. You can choose the best one you need

1. Use the powerful theme fallback property of Magento

Magento by default uses base > default package-theme directory. That means it really uses header template that is defined inside this package-theme directory. ie

 app\design\frontend\base\default\template\page\html\header.phtml

Now you need to customize header section, then the best way is, use another package-theme directory for your application. Let it be some_package > some_theme. (You can set a new package and theme for your current store through admin panel). Now what you need to do is, copy the original header.phtml file from base > default package-theme directory and paste it into your custom pacakge-theme directory. ie to this location

app\design\frontend\some_package\some_theme\template\page\html\header.phtml

You are done. Now instead of the default header.phtml, magento now will use header.phtml inside of your custom package-theme directory.

In most of the cases, this is the most recommended method for customization.

2. Use your own custom template

Now you dont need to use header.phtml, instead you need to use your own custom template for it. For this, you can use your first method. That is change the template name through layout update and then use your custom template

<reference name="header">
      <action method="setTemplate"><template>customizer/header.phtml</template></action>
</reference>

Now define your custom template file at app\design\frontend\some_package\some_theme\template\customizer/header.phtml. You are done.

3. Need to add some additional functionalities

Suppose you need to do some additional functionality. Then first define a block that will manage this additional functionality. For this you can use core/template block, if the additional changes are minor. Or use your own custom block. You can do this like this.

a. Define your block in layout update

<reference name="header">
      <block type="customizer/header" name="child.block" as="child.block" template="customizer/header.phtml" />
</reference>

Now you included you custom block that is going to manage additional features inside header block. Job is not over.

Note: you have used header as the name for your custom block.It is wrong. All block names should be unique.

B. Call your custom block inside header block's template

header block is the parent block for your custom block. It will not render it's child block content for you. Due to this, you need to manually call your block inside the header block's template file. This will allow you to manually position your block.

File : app\design\frontend\<your_package>\<your_theme>\template\page/html/header.phtml

  <div><?php echo $this->getChildHtml('child.block');  ?></div>

c. Define template that holds your custom block

Now define template for your custom block. This way magento will render your custom block's template content inside header block.

 app\design\frontend\<your_package>\<your_theme>\template\customizer/header.phtml

EDIT

so you need custom header only for a custom page. For this you can use second method itself.

File : app/design/frontend/<your_package>/<your_theme>/layout/local.xml

<customizer_index_index>
    <reference name="root">
        <block type="customizer/header" name="header" as="header">
            <action method="setTemplate"><template>customizer/header.phtml</template></action>
        </block>
    </reference>
</customizer_index_index>

Of course, you need to create Your_Customizer_Block_Header class which extends Mage_Page_Block_Html_Header as well.

Then define your custom template. Done!.

That's it. I think you are looking for the third solution right now. Go through it and ask if you have any doubts