开发者

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

开发者 https://www.devze.com 2023-03-26 12:05 出处:网络
I am trying to customize a theme using only local.xml whenever possible. I want to add a static block to the header without modifying header.phtml. This code wor开发者_Go百科ks fine for the content ar

I am trying to customize a theme using only local.xml whenever possible. I want to add a static block to the header without modifying header.phtml. This code wor开发者_Go百科ks fine for the content area, but not for the header:

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

Anybody know why? I thought that all I would need is to change “content” to “header”, but nothing shows up when I do.

Thanks for your help!


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

The header block, on the other hand, is a page/html_header block (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 render sub-blocks if their corresponding phtml template requests the block. So, by adding your block to the header, you're only doing half the work you need to. You'll need to create a custom phtml template.

The simplest way to do this (post 1.4.1.1 is to, in your own theme, create a file at

template/page/html/header.phtml

And then at the end of this file add

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

Assuming you've added a block to header block via layout xml, this should render your template.


Please Try this

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

And this code in header.phtml


add output="toHtml" in the block tag. I think it is only that.

0

精彩评论

暂无评论...
验证码 换一张
取 消