Is appending block by handle possible in Magento layout xml?

本文探讨了在Magento中使用布局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.

【事件触发一致性】研究多智能体网络如何通过分布式事件驱动控制实现有限时间内的共识(Matlab代码实现)内容概要:本文围绕多智能体网络中的事件触发一致性问题,研究如何通过分布式事件驱动控制实现有限时间内的共识,并提供了相应的Matlab代码实现方案。文中探讨了事件触发机制在降低通信负担、提升系统效率方面的优势,重点分析了多智能体系统在有限时间收敛的一致性控制策略,涉及系统模型构建、触发条件设计、稳定性与收敛性分析等核心技术环节。此外,文档还展示了该技术在航空航天、电力系统、机器人协同、无人机编队等多个前沿领域的潜在应用,体现了其跨学科的研究价值和工程实用性。; 适合人群:具备一定控制理论基础和Matlab编程能力的研究生、科研人员及从事自动化、智能系统、多智能体协同控制等相关领域的工程技术人员。; 使用场景及目标:①用于理解和实现多智能体系统在有限时间内达成一致的分布式控制方法;②为事件触发控制、分布式优化、协同控制等课题提供算法设计与仿真验证的技术参考;③支撑科研项目开发、学术论文复现及工程原型系统搭建; 阅读建议:建议结合文中提供的Matlab代码进行实践操作,重点关注事件触发条件的设计逻辑与系统收敛性证明之间的关系,同时可延伸至其他应用场景进行二次开发与性能优化。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值