Add New Reference in Magento

Featured Image

If you already performed some Magento research, you will know that it is built on a fully modular model that gives great scalability and flexibility for your store. While creating a theme, you are provided with many content blocks that you can place in structural blocks. If you are not sure what they are, please read Designer’s Guide to Magento first. Magento provides few structural blocks by default and many content blocks. This article tells what needs to be in place to create new structural block.

What are structural blocks?

They are the parent blocks of content blocks and serve to position its content blocks within a store page context. Take a look at the image below. These structural blocks exist in the forms of the header area, left column area, right column…etc. which serve to create the visual structure for a store page. Our goal is to create a new structural block called “newreference”.

newreference

Step 1: Name the structural block

Open the file layout/page.xml in your active theme folder. Inside you will find lines like:

<block type="core/text_list" name="left" as="left"/>
<block type="core/text_list" name="content" as="content"/>
<block type="core/text_list" name="right" as="right"/>

Let’s mimic this and add a new line somewhere inside the same block tag.

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

Good. Now we told Magento that new structural block exists with the name “newreference”. Magento still doesn’t know what to do with it.

Step 2: Tell Magento where to place it

We now need to point Magento where it should output this new structural block. Let’s go to template/page folder in our active theme folder. You will notice different layouts there. Let’s assume we want the new structural block to appear only on pages that use 2-column layout with right sidebar. In that case we should open 2columns-right.phtml file.

Let’s assume we wish the “newreference” block to be placed below 2 columns, but above the footer. In this case, our updated file could look like this:

<!-- start middle -->
<div class="middle-container">
<div class="middle col-2-right-layout">< ?php getChildHtml('breadcrumbs') ?>
<!-- start center -->
<div id="main" class="col-main"><!-- start global messages -->
< ?php  getChildHtml('global_messages') ?>
<!-- end global messages -->
<!-- start content -->
< ?php  getChildHtml('content') ?>
<!-- end content --></div>
<!-- end center -->

<!-- start right -->
<div class="col-right side-col">< ?php getChildHtml('right') ?></div>
<!-- end right --></div>
<div>< ?php getChildHtml('newreference') ?></div>
</div>
<!-- end middle -->

Step 3: Populating structural block

We have the block properly placed, but unfortunately nothing is new on the frontsite. Let’s populate the new block with something. We will put new products block there as an example. Go to appropriate layout XML file and add this block to appropriate place.

<reference name="newreference">
<block type="catalog/product_new" name="home.product.new" template="catalog/product/new.phtml" />
</reference>

That’s it. I hope it will help someone :)

75
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Tomislav is an owner and CEO of Inchoo. Enjoys traveling, traditional cuisine (from most cultures), good wine and strong rakija.

Other posts from this author

Discussion 75 Comments

1 2
Add Comment
  1. Christian Faulder

    disregard the spelling mistake on “sidebr.phtml”

  2. Olivier

    Any reason why this wouldnt work with 1.6.0.0??? ive been trying to do the above (trough like 12 different tutorials) for 3 days now, it f*cking wont work, im losing my mind here :(

  3. “Go to appropriate layout XML file and add this block to appropriate place.” – please clarify for the newbies of magento … I placed the code in page.xml above the following line :

    .

    I set a css border, a height and a width property for the new structural block from within 2columns-right.phtml and just got a bordered blank box in homepage .

    I just have a fresh magento 1.4.2 installatio, followed what u said and could not be successful.

    Even I inserted new products as ur code seems to be for new products to me and flushed magento cache . No result….

    Will you be kind enough to have a response ASAP?

  4. The missing line containing the code in my previous comment is :

  5. <block type="page/html_footer" name="footer" as="footer" template="page/html/footer.phtml">
    
  6. This helped me to get a fix on my problem regarding the cart page, where “left” block is removed in base layout.

    BTW very good tutorial :)
    Keep up the good work.

  7. Kaushal

    Who ever wrote this article, Just awesome. I really appreciate. It helped me a lot. I hadn’t any idea how to do this but your article helped me.

    Thanks again

  8. Amit

    I tried this for 1.6 … seems there is problem there.

  9. Finn

    You, sir, just saved my night.

  10. Jon

    Very helpful thanks! Didn’t know I had to update the page.xml file in order to put in a new reference… You saved me quite a bit of time, thanks!

  11. Mayank Batra

    HI, just a question, how can i take the menu bar out of the header and put it in another block. I dont want it to be set in the catalog.xml. Just doesn’t make sense even though it works

  12. Man you are a life safer you know that..

    When ever we are stack in something we finish always in a post you have made.

    I understand so many things today with your post.

    thanks a lot…

  13. How can i add new block inside content?

    i tried to add new ref block inside the content ref but it shows on the top of the page not where i want.

    any help would be appreciated.

  14. guy

    thankyou .. finally, FINALLY ! i understand reference / blocks / layout / xml / templates .. suddenly i see it , and i thankyou :)

  15. Amit

    thanks a lot for sharing .. i also got it finally. No issues with 1.6 as earlier stated by me .

  16. Its almost exactly what I need. But I dont know how can I create a custom method to call inside phtml like $this->customMethod();

  17. fif

    tutorial was all kosher until step 3. where’s the appropriate layout XML file? ive only really worked on page.xml but why have they made this so complex lol

  18. app/design/frontend/default/yourtheme/layout/local.xml

  19. Very helpful thanks! Didn’t know I had to update the page.xml file in order to put in a new reference… You saved me quite a bit of time, thanks!

  20. This is the best tutorial on for adding structural blocks for Magento

    Big Thumbs up!

  21. Duncan

    If I wanted to add a static block via a widget to a custom reference block, how might I go about doing that?

  22. The php in the template where you want to output the block needs to be:
    echo $this->getChildHtml(‘newreference’)
    instead of
    getChildHtml(‘newreference’)
    which throws an error

  23. Mike

    Sorry but where is the appropriate xml file in step 3? There is no: app/design/frontend/default/yourtheme/layout/local.xml

  24. @Mike feel free to create local.xml in app/design/frontend/default/yourtheme/layout/

    Take a look here: http://inchoo.net/ecommerce/magento/using-local-xml-for-overriding-or-updating-xml-structure/

    Cheers!

  25. Bob

    “Go to appropriate layout XML file and add this block to appropriate place.”

    First of all what is the “appropriate” file? Is this the local.xml?

    And why is it added there, seems a bit double to me, I already created a block in my page.xml, why should I add another block reference in another file…?

1 2

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top