Is appending block by handle possible in Magento layout xml?

In the product list on Magento category page, I have a deeply nested blocks for each product in the list. The structure is something like:

<block type="..." name="product_in_category" template="...">
 <block type="..." name="product_options_selector" template="...">
 <block type="..." name="product_price_container" template="...">
 <!-- possibly more opening tags of nestings here -->
 <block type="..." name="product_prices" template="..."/>
 <!-- closing tags of nestings -->
 </block>
 </block>
</block>

If I want to add all above blocks to product list, there is a ready solution:

<my_product_list>
 <reference name="product_list">
 <block type="..." name="product_in_category" template="...">
 <block type="..." name="product_options_selector" template="...">
 <block type="..." name="product_price_container" template="...">
 <block type="..." name="product_prices" template="..."/>
 </block>
 </block>
 </block>
 </reference>
</my_product_list>

Once I put them together as a handle, I can easily duplicate them to 3 category layouts.

<catalog_category_default>
 <update handle="my_product_list" />
</catalog_category_default>

<catalog_category_layered>
 <update handle="my_product_list" />
</catalog_category_layered>

<catalog_category_layered_nochildren>
 <update handle="my_product_list" />
</catalog_category_layered_nochildren>

So every time I make changes to product list, I only change the content in <my_product_list> and 3 category layouts get updates automatically.

It saves me a lot of time. However, since product list is also used in search result and advanced search result, product list in the result misses updates because it is named as “search_result_list” instead of “product_list”. The instance reaction is duplicating codes for “search_result_list”, i.e.

<my_product_list_of_result>
 <reference name="search_result_list">
 <block type="..." name="product_in_category" template="...">
 <block type="..." name="product_options_selector" template="...">
 <block type="..." name="product_price_container" template="...">
 <block type="..." name="product_prices" template="..."/>
 </block>
 </block>
 </block>
 </reference>
</my_product_list_of_result>

<catalogsearch_result_index>
 <update handle="my_product_list_of_result" />
</catalogsearch_result_index>

<catalogsearch_advanced_result>
 <update handle="my_product_list_of_result" />
</catalogsearch_advanced_result>

I do not like duplication, because I keep forgetting when and where the duplication is when I want to make changes. So after a think, I come up with an alternative:

<every_product>
 <reference name="product_in_category">
 <block type="..." name="product_options_selector" template="...">
 <block type="..." name="product_price_container" template="...">
 <block type="..." name="product_prices" template="..."/>
 </block>
 </block>
 </reference>
</every_product>

<add_every_product_to_category>
 <reference name="product_list">
 <block type="..." name="product_in_category" template="..."/>
 </reference>
</add_every_product_to_category>
<!-- Cannot merge to the above handle! Update handle does not work in the same handle when block is newly appended. -->
<add_every_product_to_category>
 <update handle="every_product" />
</add_every_product_to_category>

<add_every_product_to_result>
 <reference name="search_result_list">
 <block type="..." name="product_in_category" template="..."/>
 </reference>
</add_every_product_to_result>
<!-- Cannot merge to the above handle! Update handle does not work in the same handle when block is newly appended. -->
<add_every_product_to_result>
 <update handle="every_product" />
</add_every_product_to_result>

<catalog_category_default>
 <update handle="add_every_product_to_category" />
</catalog_category_default>

<catalog_category_layered>
 <update handle="add_every_product_to_category" />
</catalog_category_layered>

<catalog_category_layered_nochildren>
 <update handle="add_every_product_to_category" />
</catalog_category_layered_nochildren>

<catalogsearch_result_index>
 <update handle="add_every_product_to_result" />
</catalogsearch_result_index>

<catalogsearch_advanced_result>
 <update handle="add_every_product_to_result" />
</catalogsearch_advanced_result>

You will see the code is messy. That is why I am not satisfied with the alternative, either. I would like the layout xml file recognise something like

<my_product_layout>
 <block type="..." name="product_in_category" template="...">
 <block type="..." name="product_options_selector" template="...">
 <block type="..." name="product_price_container" template="...">
 <block type="..." name="product_prices" template="..."/>
 </block>
 </block>
 </block>
</my_product_layout>

<add_layout_to_category>
 <reference name="product_list">
 <layout handle="my_product_layout" />
 </reference>
</add_layout_to_category>

<add_layout_to_result>
 <reference name="search_result_list">
 <layout handle="my_product_layout" />
 </reference>
</add_layout_to_result>

<catalog_category_default>
 <update handle="add_layout_to_category" />
</catalog_category_default>

<catalog_category_layered>
 <update handle="add_layout_to_category" />
</catalog_category_layered>

<catalog_category_layered_nochildren>
 <update handle="add_layout_to_category" />
</catalog_category_layered_nochildren>

<catalogsearch_result_index>
 <update handle="add_layout_to_result" />
</catalogsearch_result_index>

<catalogsearch_advanced_result>
 <update handle="add_layout_to_result" />
</catalogsearch_advanced_result>

Handling the layout of product list is not the worst case. Handling the layout of order, invoice, shipment, credit note is much more time taking, becuase each document is rendered in many media, such as customer screen, admin screen, email, pdf. And if I have custom documents, and custom documents are customised for suppliers, drop shipper, etc, an easy layout update mechanism will be very handy.

In a summary, I am trying to add child blocks by handle. At mement I am intended to believe there is no way to trigger the layout xml into batch adding blocks by handle without overriding Block abstract or Layout model, but there maybe some tricky way out there. If you know it, let me know please.

3 comments

  1. Hey, have you found a solution? I’m trying to do something similar, without much success.

Leave a comment

Your email address will not be published. Required fields are marked *