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.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
ScoRpion
  • 11,364
  • 24
  • 66
  • 89

1 Answers1

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.

Dmytro Zavalkin
  • 5,265
  • 1
  • 30
  • 34
  • what is before="cart_sidebar" ? Is it template name, block name or anything else .? – ScoRpion Nov 14 '11 at 05:01
  • @Showket `before`: **Used to position the block before a block with the name specified in the value.** What is not clear here? – Dmytro Zavalkin Nov 14 '11 at 11:25
  • 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 . – ScoRpion Nov 14 '11 at 11:49
  • Thanks For giving ur precious Time...Zyava – ScoRpion Nov 14 '11 at 11:52
  • 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? – Dmytro Zavalkin Nov 14 '11 at 14:10
  • 1
    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). – Ricardo Martins Jan 18 '13 at 10:55
  • Actually, it depends on how children blocks are rendered in parent block template file. – Dmytro Zavalkin Jan 18 '13 at 16:20
  • @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. – doc_id Apr 05 '13 at 22:45
  • `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. – Pratik Joshi Aug 30 '15 at 17:09