magento2布局layout-引用Block

7天成为Magento系统架构师,现在开始学习Magento全栈开发!

《Magento2.X企业级开发实战》

magento2布局layout-引用Block

引用一个块
要更新块,请使用 指令。

示例:将图像传递给logo块。

<referenceBlock name="logo">
  <arguments>
    <argument name="logo_file" xsi:type="string">images/logo.png</argument>
  </arguments>
</referenceBlock>

向块添加新类:

<referenceBlock name="page.main.title">
    <arguments>
        <argument name="css_class" xsi:type="string">my-new-block-class</argument>
    </arguments>
</referenceBlock>

引用 CMS 块
通过使用带有 block_id 参数的 Magento/Cms/Block/Block 类将 CMS 块注入到布局中。 任何块或容器都可以用作参考。

<referenceContainer name="content.bottom">
    <block class="Magento\Cms\Block\Block" name="block_identifier">
        <arguments>
            <!-- Here is the CMS Block id -->
            <argument name="block_id" xsi:type="string">my_cms_block_identifier</argument>
        </arguments>
    </block>
</referenceContainer>

结果,CMS 块添加到页面底部。

使块可见性动态
可以使用 ifconfig 参数根据 Magento/Config/Model/Config/Source/Yesno 系统配置字段将任何块配置为显示或不显示。 对于该值,请使用 XPath 到所需字段。

<block class="Namespace\Module\Block\Type" name="block.example" ifconfig="my/yesno/field">
    ...
</block>

也可以使用 ACL 资源调整可见性。 虽然它主要用于管理区域,但同样的方法也适用于店面。

<block class="Namespace\Module\Block\Type" name="block.example" aclResource="Vendor_ModuleName::acl_name">
    <!-- ... -->
</block>

设置块使用的模板
可以通过三种方式为块设置模板:

使用模板属性
使用 指令
使用 指令
以下示例演示了每种方法:

示例 1:使用模板属性

<referenceBlock name="page.main.title" template="%Namespace_Module::new_template.phtml%"/>

使用 示例 2:使用 arguments指令

 <referenceBlock name="page.main.title">
   <arguments>
     <argument name="template" xsi:type="string">%Namespace_Module::new_template.phtml%</argument>
   </arguments>
 </referenceBlock>
示例 3:使用 <action method="setTemplate"> 指令
 <referenceBlock name="page.main.title">
   <action method="setTemplate">
     <argument name="template" xsi:type="string">%Namespace_Module::new_template.phtml%</argument>
   </action>
 </referenceBlock>

在上面的示例中,模板是根据以下内容指定的:

Namespace_Module:定义模板所属的模块。 例如,Magento_Catalog。
new_template.phtml:模板相对于模板目录的路径。 例如:/view//templates 或 //templates。

优先级最高的模板是带有 setTemplate action <action method="setTemplate"> 的模板。 第二优先级具有指定为 <referenceBlock name="..." template="..."/> 的属性,最低优先级具有使用 <argument> 的模板。
这意味着,如果对于某个块,将模板设置为属性,它将覆盖您在 <argument> 中为同一块指定的值。 在我们有 <action method="setTemplate"> 构造的情况下 - 它将覆盖您在模板属性中指定的值 <referenceBlock name="..." template="..."/> 和您的值 使用 <argument> 指定。

如无特殊说明或标注,任何个人或组织,复制、转载、采集本站内容请注明:
本文来源于:【Magento中文网】,并添加本文地址链接。
如未按上述操作复制或转载,本站有权追究法律责任。
若本站内容侵犯了原著者的合法权益,可联系我们进行处理。