tags:

views:

1503

answers:

3

I am using this code in my template file to display a static block in my left sidebar:

<?= $this->getLayout()->createBlock('cms/block')->setBlockId('leftSB1')->toHtml() ?>

I would like to exclude the block from one of my CMS pages. How do I do this?

I think it requires adding code to the 'Layout Update XML' section but I'm not sure what exactly.

+2  A: 

Someone else can correct me here, but I'm fairly sure that you're going to have trouble trying to accomplish this given the way you called the block. Normal layout updates allow you to remove blocks, but those are blocks that were also created with the layout (e.g. the Layout object knows about them after you call loadLayout()).

In your case, you create the block on the fly and then immediately use it to echo some HTML. If you want to be able to delete it with layout updates, try moving it into the layout files first, then use the normal layout block removal method:

<reference name="your_parent_block_name">
   <remove name="leftSB1"/>
</reference>

Otherwise, you could hide it either in the PHP (By setting some global variable and checking it before outputting the block. Poor form but it might work.) or in CSS. Let me know if any of these work for you.

Thanks, Joe

Joseph Mastey
+1  A: 

Include the block in your layout instead:

<cms_page>
  <reference name="left">
    <block type="cms/block" name="leftSB1">
      <action method="setBlockId"><id>leftSB1</id></action>
    </block>
  </reference>
</cms_page>

And then $this->getChildHtml('leftSB1') in your sidebar, if you're not including children automatically.

(and then remove it from the particular page as in the previous answer)

Greg
Which layout file would I include it in so that it showed up in the left sidebar of every page? This is the reason I added it to the template file because I could find a layout file which would add it everywhere. Thanks.
a1anm
all layout files are examined for all pages, so it doesn't matter - it's the "handle" that determines what's shown on a particular page, that's the <[module]_[controller]_[action]> bit ([action] is optional, as with cms_page, as there's only a 'view' action that does anything). cms.xml would make the most sense, and if someone else needs to find it, that would probably be the first place they'd look.
Greg
Doing this then displays the block on my CMS pages but not on my Category pages. This is the problem I had before.....Adding to the layout files only seems to add to specific pages and not all pages.
a1anm
put it in <default> instead then
Greg
A: 

Thanks, Joe, that worked perfectly for me!