2

What is the best practice for the given situation?

I'm creating an extension and following Magento's best practices by creating my layout and templates in base/default but putting them in the module folders..etc. One of the requirements of the extension though, is to add something into the header template about where the default welcome message is. I don't want to change the core header template and I can't create my own theme because it's an extension. I'm not sure just adding a block to 'header' would work. What can you do?

2

In the default header.phtml there is this line:

<p class="welcome-msg"><?php echo $this->getWelcome() ?> <?php echo $this->getAdditionalHtml() ?></p>

From my point of view you have 3 options.

Option 1. In case you want to add a simple text that does not depend on the page or user and you want to add it after the welcome message you can add this piece of code in your layout file:

<reference name="header">
    <action method="setAdditionalHtml"><additional>YOUR TEXT HERE</additional></action>
</reference>

Option 2. If you want to add user/page dependent content or you want to add the content before the welcome message you can override this method Mage_Page_Block_Html_Header::getWelcome() and make it look something like this:

public function getWelcome(){
   $text = 'YOUR TEXT HERE';
   return $text.parent::getWelcome();
}

Option 3. Put your block content in the footer. This can be done easily with

<reference name="footer">
    <block type=".." name=".." as=".." template=".." />
</reference>

And in your block include a javascript that moves your content to the desired header area. It's not the prettiest way to do it but it should work. The downside of this is that it may not work on a custom theme. But you can include an explanation on how to make it work on your readme file.

| 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.