我们在做magento2 模块开发的时候,有时会对另一个模块进行添加,修改,和删除,编辑等等。。。,这里我们就要使用布局layout的功能了,layout的xml配置文件可以让你无所不能,非常利于代码的优化。
接下来我就讲讲layout中xml的配置文件
首先举个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?xml version="1.0"?> <!-- /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ 此文件名是/Qiliq/Post/view/frontend/layout/customer_address_form.xml --> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <update handle="customer_account"/> <body> <referenceContainer name="content"> <block class="Qiliq\Post\Block\Postcode\Ajax\View" name="jp_postcode_view" template="Qiliq_Post::postcode/ajax/view.phtml" after="-" /> </referenceContainer> </body> </page> |
接下来我详细讲解layout的功能。
update:标签是合并其他的句柄layout的功能,即使合并handle所指的句柄,有人说是钩子都是可以的,只是叫法不同实质都是一样的。
handle:就是所指的句柄,就是layout的xml前的名字 (例如customer_address_form.xml)即使指的是customer_address_form这一部分,当然也有可能是你定义的controller的action方法,都称为句柄
refercenContainer:指的就是定义好的container 下面的代码即使定义好的container元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <?xml version="1.0"?> <!-- /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ --> <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_layout.xsd"> <container name="root"> <container name="backend.page"> <container name="after.body.start" as="after.body.start" label="Page Top" before="-"/> <container name="page.wrapper" as="page_wrapper" htmlTag="div" htmlClass="page-wrapper"> <container name="notices.wrapper" before="-" htmlClass="notices-wrapper" htmlTag="div"> <container name="notifications" as="notifications" /> <container name="notification.window" as="notification_window" before="-"/> <container name="global.notices" as="global_notices" after="notification.window"/> </container> <container name="header" htmlTag="header" htmlClass="page-header"/> <container name="page.menu" as="page.menu" after="header"/> <container name="page.breadcrumbs" as="page.breadcrumbs"/> <container name="page.formkey" as="page.formkey"/> <container name="page.content" as="page_content" htmlTag="main" htmlId="anchor-content" htmlClass="page-content"> <container name="main.top" as="main-top" label="main-top"/> <container name="page.main.actions" as="page_main_actions" htmlTag="div" htmlClass="page-main-actions"/> <container name="messages.wrapper" as="messages.wrapper" htmlTag="div" htmlId="messages"> <container name="page.messages" as="page.messages"/> </container> <container name="page.main.container" as="page_main_container" htmlId="page:main-container" htmlTag="div" htmlClass="page-columns"> <container name="admin.scope.col.wrap" as="admin-scope-col-wrap" htmlTag="div" htmlClass="admin__old"> <!-- ToDo UI: remove this wrapper remove with old styles removal --> <container name="main.col" as="main-col" htmlId="container" htmlTag="div" htmlClass="main-col"> <container name="content" as="content"/> </container> </container> </container> </container> <container name="js" as="js" label="JavaScript"/> <container htmlTag="footer" htmlClass="page-footer"> <container name="footer" htmlTag="div" htmlClass="page-footer-content row" /> </container> </container> <container name="before.body.end" as="before_body_end" label="Before Body End" after="-"/> </container> </container> </layout> |
接下来讲解block标签
1 | <block class="Qiliq\Post\Block\Postcode\Ajax\View" name="jp_postcode_view" template="Qiliq_Post::postcode/ajax/view.phtml" after="-" /> |
block:是定义好的类block块
class: 指的是类Qiliq\Post\Block\Postcode\Ajax\View.php
name:是指的是给这个block这个类的名字
as:指的这个类的别名
template:指的是调用的template模块Qiliq_Post::postcode/ajax/view.phtml
after:指的是在上一个block之后
掌握了这个layout就可以让你在block里面随意改变。
希望对你有帮助
又不懂的地方请留言。