Search code examples
templateslayoutmagentoreferenceblock

Provide new references for static blocks in Magento layout


I want to display a carousel and two banners at the top of each page content of my site. I’ve created a custom reference starting from the footer block in page.xml. So this is what it looks like:

<block type="page/html" name="topcontent" as="topcontent" template="page/html/topcontent.phtml">
    <block type="page/html_wrapper" name="topcontent.container" as="topcontentContainer" translate="label">
        <label>Page Top Content</label>
        <action method="setElementClass"><value>topcontent-container</value></action>
    </block>
    <block type="core/template" name="topcontent.book.carousel" as="topcontentCarousel" template="callouts/book-carousel.phtml"/>
    <block type="core/text_list" name="topcontent.left" as="topcontentLeft" />
    <block type="core/text_list" name="topcontent.right" as="topcontentRight" />
</block>

Then I created a topcontent.phtml file where I put

<div class="topcontent-container">
    <div class="topcontent">
      <?php echo $this->getChildHtml('topcontentContainer') ?> 
      <?php echo $this->getChildHtml('topcontentCarousel') ?>
      <?php echo $this->getChildHtml('topcontentLeft') ?>
      <?php echo $this->getChildHtml('topcontentRight') ?>
    </div>
</div>

I have my carousel displayed correctly, but when I try to put a block in topcontentLeft or topcontentRight, it is not displayed ad all. I think I’m doing something wrong with the block type parameter, but I can’t figure out what: can someone provide me some help? Thanks.


Solution

  • Depends on what you need to display in banner. If it's just some text you could use text block:

    <block type="core/text" name="topcontent.right" as="topcontentRight">
        <action method="addText"><text>Test text</text></action>
    </block>
    

    If you need placeholder block to show some CMS static block content, then you're right, cote/text_list is appropriate type for such block. It takes all it's nested blocks and render them one by one. So next you need to do is to put cms/block placeholder, its content could be added later from backend. All together it may looks like:

    <block type="core/text_list" name="topcontent.right" as="topcontentRight">
        <block type="cms/block" name="topcontent.right.cms" as="topcontentRightCms">
            <action method="setBlockId"><block_id>topcontent_right_static</block_id></action>
        </block>
    </block>
    

    Now you could create new static bloc in admin backend with 'topcontent_right_static' id and it'll be rendered in place where you output it.