1

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)
  • 1
    i suggest.please rewrite the class of Mage_Page_Block_Html_Header and add function – Amit Bera Oct 2 '14 at 6:32
  • @AmitBera This is the last option if others cant. – Tran Dinh Khanh Oct 2 '14 at 6:49
  • are want to call Mage_Page_Block_Html_Header block which include extra function and to you want to remove default..blocks of header – Amit Bera Oct 2 '14 at 6:52
  • @AmitBera exactly! I want to add my custom functions to Mage_Page_Block_Html_Header, and want to use completely different header file. (I just update my question, you can read update part.) – Tran Dinh Khanh Oct 2 '14 at 6:54
  • also want to remove defaut header? – Amit Bera Oct 2 '14 at 6:58
3

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

  • Thank you for awesome answer. I just update my question (to reply yours). My need is mixup your 2nd and 3rd option. I want to use completely different header file because it should just apply to specific handle (then cannot use child blocks). I also need to add some functions so it needs code behind the scene. – Tran Dinh Khanh Oct 2 '14 at 6:48
  • 1
    oh got it. i will now update my answer – Rajeev K Tomy Oct 2 '14 at 7:08
  • 1
    answer updated. See the edit section. You just remove the existing header and then declare your custom block as new header block. Make sure your block should extend from default header block, as you mentioned int the question – Rajeev K Tomy Oct 2 '14 at 7:11
  • 1
    oh I forgot that header block comes inside root block. that was the mistake from my part :) happy to help you – Rajeev K Tomy Oct 2 '14 at 7:27
  • 1
    updates as per your request. Please remove answer from your question. If you need, put it as an answer then accept it. Enrich our community with good practice – Rajeev K Tomy Oct 2 '14 at 7:29

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.