Before And After In Local.xml Magento?
Asked Answered
Y

1

6

I've managed to get to the point where I understand how to move blocks from column to column in my Magento layouts: via specifying a "left" or "right" attribute in the <reference> tag. However, I don't understand how to change the order in which blocks appear. I've noticed that the "before" and "after" attributes of the <block> tag have something to do it with, but I'm not sure how they work. If I want to move a block from the top of its area to anywhere else in our page, what's the proper use and syntax for those attributes?

For example, I have a Category page and I have these blocks in it:

  • view.phtml
    • list.phtml
      • toolbar.phtml

... and so on.

I want to put my block anywhere within these blocks, or at the top of these blocks, or make all of these blocks show up inside another block. How can I use "before" and "after" to achieve this using my local.xml file?

Note: I can do it manually by inheriting their respective .xml files, but that's not a good solution to the problem as a whole.

Yolande answered 11/11, 2011 at 11:53 Comment(0)
E
7

before: Used to position the block before a block with the name specified in the value. If "-" value used the block is positioned before all other blocks of its level of block nesting.

after: Used to position the block after a block with the name specified in the value. If "-" value used the block is positioned after all other blocks of its level of block nesting.

Updated: examples from some random core layout updates:

<reference name="right">
    <block type="catalog/product_compare_sidebar" before="cart_sidebar" name="catalog.compare.sidebar" template="catalog/product/compare/sidebar.phtml"/>
</reference>

<reference name="right">
    <block type="catalog/product_list_related" name="catalog.product.related" before="-" template="catalog/product/list/related.phtml"/>
</reference>

Updated: I believe before and after work only in core/text_list and similar(descendant) blocks, i.e. blocks which just render blocks their nested blocks.

Ergotism answered 12/11, 2011 at 18:58 Comment(9)
what is before="cart_sidebar" ? Is it template name, block name or anything else .?Yolande
@Showket before: Used to position the block before a block with the name specified in the value. What is not clear here?Ergotism
I am Not understanding If I am writing before = "Some Block Name" Eg In Above Example if i want the above second block Before the First one Do I need to Write it Something Like This . <reference name="right"> <block type="catalog/product_list_related" name="catalog.product.related" before="catalog.compare.sidebar" template="catalog/product/list/related.phtml"/> </reference>Yolande
Thanks For giving ur precious Time...ZyavaYolande
Blocks order is as they declared within parent block/handle. If you wish to change blocks order in your theme/module without editing core layout updates - you should use after/before attributes. Make sense?Ergotism
And I'm almost sure that after and before only works inside blocks of type *_list. I.e: core/text_list type blocks, like "right", "top.menu" blocks. I didn't make it work with blocks like "footer" (after="footer or after="-" using footer as reference).Polychrome
Actually, it depends on how children blocks are rendered in parent block template file.Ergotism
@RicardoMartins your note saved me from reading all the core code to know what's wrong with my XML layout, however I hardly noticed this tiny comment. Such useful info should be added as an answer even if not chosen, but would be better indexed.Abutilon
Updated: examples from some random core layout updates:, you Didn't explain anything using those examples. Please edit and let us all know how the example uses after and before. Thanks anyways.Cruiser

© 2022 - 2024 — McMap. All rights reserved.