Jimmery
Jimmery

Reputation: 10139

The tale of Magento and its XML Layout

Once upon a time, in an Magento 1.9.1 layout XML file...

    <!-- THIS ADDS A BANNER: -->
    <reference name="content">
        <block type="cms/block" name="blog_banner1">
            <action method="setBlockId"><block_id>blog_banner</block_id></action>
        </block>
    </reference>

    <!-- ...BUT NONE OF THESE ADD ANY BANNERS: -->
    <reference name="above_main">
        <block type="cms/block" name="blog_banner2">
            <action method="setBlockId"><block_id>blog_banner</block_id></action>
        </block>
    </reference>
    <reference name="breadcrumbs">
        <block type="cms/block" name="blog_banner3">
            <action method="setBlockId"><block_id>blog_banner</block_id></action>
        </block>
    </reference>
    <reference name="global_messages">
        <block type="cms/block" name="blog_banner4">
            <action method="setBlockId"><block_id>blog_banner</block_id></action>
        </block>
    </reference>
    <!-- if adding a banner to "above_main" would work that would be nice... :( -->

...On a page, far, far away (2columns-left.phtml)...

<body<?php echo $this->getBodyClass()?' class="'.$this->getBodyClass().'"':'' ?>>
<?php echo $this->getChildHtml('after_body_start') ?>

<?php echo $this->getChildHtml('right_panel') ?>
<div class="wrapper">
    <?php echo $this->getChildHtml('global_notices') ?>
    <div class="page container">
        <?php echo $this->getChildHtml('header') ?>
        <div class="container body-wrapper">
            <?php echo $this->getChildHtml('global_messages') ?>
            <?php echo $this->getChildHtml('breadcrumbs') ?>
            <?php echo $this->getChildHtml('above_main') ?>
            <div class="row">
                <div class="sidebar col-sm-3"><?php echo $this->getChildHtml('left') ?></div>
                <div class="col-sm-9">
                    <?php echo $this->getChildHtml('content') ?>
                </div>
            </div>
        </div>
        <?php echo $this->getChildHtml('footer') ?>
        <?php echo $this->getChildHtml('global_cookie_notice') ?>
        <?php echo $this->getChildHtml('before_body_end') ?>
    </div>
</div>
<?php echo $this->getAbsoluteFooter() ?>
</body>

The mystery of the same layout rules working on content, but not on above_main, breadcrumbs or global_messages.

Can anyone shed any light on this so I can live happily ever after?

Upvotes: 2

Views: 48

Answers (1)

PixieMedia
PixieMedia

Reputation: 1548

All your blocks have the same name, you cant have more than 1 block element on the same page with the same name, it will ignore the duplicates. Change the name to

name="blog_banner_1"
name="blog_banner_2"

etc, and the force is strong.

Also, make sure you have defined the above_main block in your page.xml. In the 'default' block of page.xml, find;

 <block type="core/text_list" name="right" as="right" translate="label">
            <label>Right Column</label>
        </block>

and immediately after it, add this;

<block type="core/text_list" name="above_main" as="above_main"/>

Now you have created the page reference above_main so hopefully you will be able to add your block to it.

The reason you couldnt add to reference 'breadcrumbs' is clear when you look at the page.xml too -

<block type="core/text_list" name="breadcrumbs.container" as="breadcrumbsContainer" translate="label">
            <label>Breadcrumbs</label>
            <block type="page/html_breadcrumbs" name="breadcrumbs"/>
        </block>

Only blocks of type "core/text_list" will render children when called as a block reference - so all reference="blah" in your custom XML must be made to reference the containing block of type core/text_list - in the breadcrumb exmaple, it should be;

<reference name="breadcrumbs.container">
<block type="cms/block" name="blog_banner2">
        <action method="setBlockId"><block_id>blog_banner</block_id></action>
    </block>
</reference>

Now take a look at global_messages;

<block type="core/messages" name="global_messages" as="global_messages"/>

This can't be used as a container reference for adding children as it will never render them.

Upvotes: 2

Related Questions