In Magento, can I only add a static block to the header using xml?

I am trying to customize the theme using only local.xml when possible. I want to add a static block to the header without changing header.phtml. This code is great for the content area, but not for the title:

<default> <reference name="content"> <block type="cms/block" name="how-it-works-button"> <action method="setBlockId"><block_id>how-it-works</block_id></action> </block> </reference> </default> 

Does anyone know why? I thought that all I needed to do was change the โ€œcontentโ€ to the โ€œtitleโ€, but nothing appears when I do this.

Thank you for your help!

+4
source share
3 answers

The content block is a special block known as the core/text_list (PHP class Mage_Core_Block_Text_List ). These blocks will automatically display any child blocks that are added to them.

The header block, on the other hand, is a page/html_header (PHP class Mage_Page_Block_Html_Header ). This block class inherits from Mage_Core_Block_Template , making it a core/template block. Template blocks will only display sub blocks if their corresponding phtml template requests a block. So, adding your block to the header, you only do half the work you need. You need to create your own phtml template .

The easiest way to do this (post 1.4.1.1 - in your own theme, create a file in

 template/page/html/header.phtml 

And then at the end of this file add

 <?php echo $this->getChildHtml('how-it-works-button'); ?> 

Assuming you added a block to the title block through the xml layout, this should display your template.

+4
source

Please try this

 <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('how-it-works')->toHtml() ?> 

And this code in header.phtml

+1
source

add output = "toHtml" in the block tag. I think this is the only way.

0
source

All Articles