Is appending block by handle possible in Magento layout xml?

 

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:

1 < block type = "..." name = "product_in_category" template = "..." >
2   < block type = "..." name = "product_options_selector" template = "..." >
3   < block type = "..." name = "product_price_container" template = "..." >
4   <!-- possibly more opening tags of nestings here -->
5   < block type = "..." name = "product_prices" template = "..." />
6   <!-- closing tags of nestings -->
7   </ block >
8   </ block >
9 </ block >

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

01 < my_product_list >
02   < reference name = "product_list" >
03   < block type = "..." name = "product_in_category" template = "..." >
04   < block type = "..." name = "product_options_selector" template = "..." >
05   < block type = "..." name = "product_price_container" template = "..." >
06   < block type = "..." name = "product_prices" template = "..." />
07   </ block >
08   </ block >
09   </ block >
10   </ reference >
11 </ my_product_list >

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

01 < catalog_category_default >
02   < update handle = "my_product_list" />
03 </ catalog_category_default >
04  
05 < catalog_category_layered >
06   < update handle = "my_product_list" />
07 </ catalog_category_layered >
08  
09 < catalog_category_layered_nochildren >
10   < update handle = "my_product_list" />
11 </ 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.

01 < my_product_list_of_result >
02   < reference name = "search_result_list" >
03   < block type = "..." name = "product_in_category" template = "..." >
04   < block type = "..." name = "product_options_selector" template = "..." >
05   < block type = "..." name = "product_price_container" template = "..." >
06   < block type = "..." name = "product_prices" template = "..." />
07   </ block >
08   </ block >
09   </ block >
10   </ reference >
11 </ my_product_list_of_result >
12  
13 < catalogsearch_result_index >
14   < update handle = "my_product_list_of_result" />
15 </ catalogsearch_result_index >
16  
17 < catalogsearch_advanced_result >
18   < update handle = "my_product_list_of_result" />
19 </ 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:

01 < every_product >
02   < reference name = "product_in_category" >
03   < block type = "..." name = "product_options_selector" template = "..." >
04   < block type = "..." name = "product_price_container" template = "..." >
05   < block type = "..." name = "product_prices" template = "..." />
06   </ block >
07   </ block >
08   </ reference >
09 </ every_product >
10  
11 < add_every_product_to_category >
12   < reference name = "product_list" >
13   < block type = "..." name = "product_in_category" template = "..." />
14   </ reference >
15 </ add_every_product_to_category >
16 <!-- Cannot merge to the above handle! Update handle does not work in the same handle when block is newly appended. -->
17 < add_every_product_to_category >
18   < update handle = "every_product" />
19 </ add_every_product_to_category >
20  
21 < add_every_product_to_result >
22   < reference name = "search_result_list" >
23   < block type = "..." name = "product_in_category" template = "..." />
24   </ reference >
25 </ add_every_product_to_result >
26 <!-- Cannot merge to the above handle! Update handle does not work in the same handle when block is newly appended. -->
27 < add_every_product_to_result >
28   < update handle = "every_product" />
29 </ add_every_product_to_result >
30  
31 < catalog_category_default >
32   < update handle = "add_every_product_to_category" />
33 </ catalog_category_default >
34  
35 < catalog_category_layered >
36   < update handle = "add_every_product_to_category" />
37 </ catalog_category_layered >
38  
39 < catalog_category_layered_nochildren >
40   < update handle = "add_every_product_to_category" />
41 </ catalog_category_layered_nochildren >
42  
43 < catalogsearch_result_index >
44   < update handle = "add_every_product_to_result" />
45 </ catalogsearch_result_index >
46  
47 < catalogsearch_advanced_result >
48   < update handle = "add_every_product_to_result" />
49 </ 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

01 < my_product_layout >
02   < block type = "..." name = "product_in_category" template = "..." >
03   < block type = "..." name = "product_options_selector" template = "..." >
04   < block type = "..." name = "product_price_container" template = "..." >
05   < block type = "..." name = "product_prices" template = "..." />
06   </ block >
07   </ block >
08   </ block >
09 </ my_product_layout >
10  
11 < add_layout_to_category >
12   < reference name = "product_list" >
13   < layout handle = "my_product_layout" />
14   </ reference >
15 </ add_layout_to_category >
16  
17 < add_layout_to_result >
18   < reference name = "search_result_list" >
19   < layout handle = "my_product_layout" />
20   </ reference >
21 </ add_layout_to_result >
22  
23 < catalog_category_default >
24   < update handle = "add_layout_to_category" />
25 </ catalog_category_default >
26  
27 < catalog_category_layered >
28   < update handle = "add_layout_to_category" />
29 </ catalog_category_layered >
30  
31 < catalog_category_layered_nochildren >
32   < update handle = "add_layout_to_category" />
33 </ catalog_category_layered_nochildren >
34  
35 < catalogsearch_result_index >
36   < update handle = "add_layout_to_result" />
37 </ catalogsearch_result_index >
38  
39 < catalogsearch_advanced_result >
40   < update handle = "add_layout_to_result" />
41 </ 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.

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值