3

I want to add a custom block to the category pages and not care if the the category is anchor or not or if it has children or not.
I want to add that block at the bottom of the page after the product list or static block.
So I added this in the layout file

<catalog_category_view>
    <reference name="content">
        <block type="some/block" tempalate="some/template.phtml" after="-" /><!-- notice the "after" attribute -->
     </reference>
</catalog_category_view>

the block appears in every category page but at the top of the page.
If I change the layout handle to catalog_category_default or catalog_category_layered everything works OK and I see my block at the bottom of the page.
Any idea why "after" is ignored for catalog_category_view handle?
Note: Using the default magento theme

  • Seems like different order of block rendering. Some other children of content block of catalog_category_view is rendered after your block. – user487772 Jan 21 '14 at 9:09
  • Good question by the way. Never head of catalog_catagory_view layout handle before. – user487772 Jan 21 '14 at 9:11
  • Yep, but the question is why? I didn't see any after="-" on other blocks in all the category page layout handles. As for the layout handle name catalog_category_view is the default one generated for all the pages through $this->addActionLayoutHandles(); – Marius Jan 21 '14 at 9:12
  • Yes I also heard catalog_catagory_view but I have done via code but via xml code. good questions. – Keyul Shah Jan 21 '14 at 9:13
  • 1
    @KeyulShah. I don't want to do that, since the last block can be different in theory. I want my block "absolute last". I also know how to do that. I can create a custom handle and use <update handle="my_handle" /> inside catalog_category_default and catalog_category_layered. But I'm just curious to why this doesn't work. – Marius Jan 21 '14 at 9:16
3

I think I've isolated the problem.
The catalog_category_view layout handle is loaded before the _layered or _default handle and the block are generated in the order they are found. So my custom block is generated and inserted into the content before the category/view block found in the _layered handle.
So the 'after' attribute is taken into consideration and my block is added last at that point.
But then comes the _layered layout handle and because the category/view block does not have a before or after specified it is inserted like this:

$parentBlock->append($block, $alias);

That is the same thing as

$this->insert($block, '', true, $alias);

The same code is executed when after="-" is specified. So it's like category/view has after="-" specified.
This does not occur when the blocks are in the same layout handle, because usually the custom module layout handle is loaded after the one from catalog.xml.

1

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.

  • This will not work in some cases (obviously the case of a topic starter). This is a known problem of Magento 1.x and will be fixed in 2.x. – user487772 Jan 21 '14 at 9:29
  • Thanks for the answer. I know what before and after should do and it should work for all the block types, not only core/text_list. Like I've said in the question, it works if I change the layout handle name. – Marius Jan 21 '14 at 9:32
  • ok Marius and Tim :) – Keyul Shah Jan 21 '14 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.