Magento 1.9 Custom Reference – Fix Custom Reference Not Showing

layoutmagento-1.9

I try to add block to my template and I want to use reference, to my Magento 1.9.1.
So I did like this in local.xml:

<reference name="root">
    <block type="core/text_list" name="customerLinks" as="customerLinks" translate="label">
    <label>Customer Links</label>
    </block>
</reference>
<reference name="customerLinksReference">
    <block type="page/html" name="customerLinksReference" template="page/html/customerlinks.phtml" />
</reference>

…and to header.phtml:

<?php echo $this->getChildHtml('customerLinks') ?>

and add customerlinks.phtml to folder: app/design/frontend/crazytee/default/template/page/html
(in block template I try with and without page/html)

I have switched off all caches.
I got this from here: http://inchoo.net/magento/custom-reference-structural-block/

Best Answer

Here I assume all of your code comes inside default layout handle and hence the snippet that you have shown us is enclosed in

<layout>
    <default> <!-- layout handle for almost all pages -->

         <!-- your code snippet comes here. -->

    <default>
</layout>

With that sais, let us analyse your layout definitions.

First you are trying to define a new structural block with this code.

<reference name="root">
    <block type="core/text_list" name="customerLinks" as="customerLinks" translate="label">
        <label>Customer Links</label>
    </block>
</reference>

STATUS : SUCCESS

COMMENTS : Beautiful !. You have done it right and let's give a thanks to great inchooers at this moment :-)

Then you are trying to add a new block inside your custom block with name customerLinks with this code snipper

 <reference name="customerLinksReference">
    <block type="page/html" name="customerLinksReference" template="page/html/customerlinks.phtml" />
 </reference>

STATUS : FAILED

COMMENTS : You are referencing to a block with name customerLinksReference here. Magento do not know about this block, unless you didnt define a block with name customerLinksReference before local.xml get processed. Your custom block name is customerLinks and hence your block definition should look like this.

 <reference name="customerLinks">
    <block type="page/html" name="customerLinksReference" template="page/html/customerlinks.phtml" />
 </reference>

Now look on the block that you have included in your custom block. I am talking about the block with name customerLinksReference and is of type page/html. Now page/html stands for the block instance Mage_Page_Block_Html and it is designed by Magento team to hold page definition.ie it is the block which holds structural blocks of a page and these structural blocks together constitutes the entire page structure. Actually root block is of this type.

So my point is we dont want this big block for our need. We have another block Mage_Core_Block_Template which we will use normally to insert some html contents. If the contents in your block is not that much complex, this is the perfect block that you need to use. (otherwise you need to go for custom block). So let us use this type of block here. So your code now will look like

<reference name="customerLinks">
    <block type="core/template" name="customerLinksReference" template="page/html/customerlinks.phtml" />
 </reference>

Lastly, you need to call your block manually in page structure. You have called your custom block like this.

<?php echo $this->getChildHtml('customerLinks') ?>

STATUS : SUCCESS

COMMENTS : Great ! you have called your block correctly inside..... oh wait....

STATUS : AGAIN FAILURE

COMMENTS : You have called your block in wrong place. It should call inside root template file. What is root template files ? In short, they are phtml files which is used to structure the page. You can find them inside app\design\frontend\<your_package>\<your_theme>\template\page/. Examples are 1column.phtml, 2columns-left.phtml etc. You should call your block inside these phtml files. More specifically phtml which is used by your magento application. For great flexibility, I recommend you to call your block in every root template files.

I hope you understood the mistakes that you have done.

Actually inchooers said it right. You heard it wrong :-)

Related Topic