zero1/layout-xml-plus

使用布局XML拦截块HTML输出,大多数情况下无需覆盖模板。

1.0.4 2024-09-12 11:02 UTC

This package is auto-updated.

Last update: 2024-09-12 11:02:37 UTC


README

此模块旨在减少覆盖块模板的需求。例如,更改按钮的单个类不需要您覆盖模板。然而,对于重要的DOM结构更改,我们仍然建议覆盖模板。

安装

composer require module enable setup:upgrade

操作

功能已被拆分为“操作”,这些操作可以在将块HTML输出传递给用户之前执行。

常见参数

  • action:要执行的操作的ID
  • xpath:一个XPath表达式,用于识别模板中的元素。为了兼容性,所有模板都在一个<root>节点内渲染,因此如果您想要第一个div,您将需要类似(/root/div)[1]的东西或第二个div:(/root/div)[2]或所有div:/root//div

AttributeValueReplace

id:attribute_value_replace替换属性值内的值,一个好的例子是用另一个类替换一个类。

示例 catalog_category_view.xml

<referenceBlock name="category.products.list">
    <arguments>
        <argument name="after_html_actions" xsi:type="array">
            <item name="replace-padding" xsi:type="array">
                <item name="xpath" xsi:type="string"><![CDATA[/root//section[@id="product-list"]]]></item>
                <item name="action" xsi:type="string">attribute_value_replace</item>
                <item name="attribute" xsi:type="string">class</item>
                <item name="search" xsi:type="string">py-8</item>
                <item name="replace" xsi:type="string">pb-8</item>
            </item>
        </argument>
    </arguments>
</referenceBlock>

AttributeValueSet

id:attribute_value_set

完全覆盖属性的值

示例 catalog_category_view.xml

<referenceBlock name="category.products.list">
    <arguments>
        <argument name="after_html_actions" xsi:type="array">
            <item name="change-wrapper-id" xsi:type="array">
                <item name="xpath" xsi:type="string"><![CDATA[/root//section[@id="product-list"]]]></item>
                <item name="action" xsi:type="string">attribute_value_set</item>
                <item name="attribute" xsi:type="string">id</item>
                <item name="value" xsi:type="string">product-list-wrapper</item>
            </item>
        </argument>
    </arguments>
</referenceBlock>

AttributeValueAppend

id:attribute_value_append

将项目添加到属性的值列表中(例如添加一个类)

示例 catalog_product_view.xml

<referenceBlock name="product.detail.page">
    <arguments>
        <argument name="after_html_actions" xsi:type="array">
            <item name="add-section-class" xsi:type="array">
                <item name="xpath" xsi:type="string"><![CDATA[/root//section]]></item>
                <item name="action" xsi:type="string">attribute_value_append</item>
                <item name="attribute" xsi:type="string">class</item>
                <item name="value" xsi:type="string">c-pdp-container</item>
            </item>
        </argument>
    </arguments>
</referenceBlock>

示例 catalog_product_view.xml

<referenceBlock name="product.detail.page">
    <arguments>
        <argument name="after_html_actions" xsi:type="array">
            <item name="add-classes" xsi:type="array">
                <item name="xpath" xsi:type="string"><![CDATA[(/root/section/div/div)[1]]]></item>
                <item name="action" xsi:type="string">attribute_value_append</item>
                <item name="attribute" xsi:type="string">class</item>
                <item name="value" xsi:type="array">
                    <item name="flex" xsi:type="string">flex</item>
                    <item name="flex-wrap" xsi:type="string">flex-wrap</item>
                    <item name="order-first" xsi:type="string">order-first</item>
                </item>
            </item>
        </argument>
    </arguments>
</referenceBlock>

AttributeValueRemove

id:attribute_value_remove从属性的值列表中移除值

示例 catalog_product_view.xml

<referenceBlock name="product.detail.page">
    <arguments>
        <argument name="after_html_actions" xsi:type="array">
            <item name="remove-classes" xsi:type="array">
                <item name="xpath" xsi:type="string"><![CDATA[(/root/section/div/div)[1]]]></item>
                <item name="action" xsi:type="string">attribute_value_remove</item>
                <item name="attribute" xsi:type="string">class</item>
                <item name="value" xsi:type="array">
                    <item name="grid" xsi:type="string">grid</item>
                    <item name="grid-rows-auto" xsi:type="string">grid-rows-auto</item>
                    <item name="grid-cols-1" xsi:type="string">grid-cols-1</item>
                    <item name="md:gap-x-5" xsi:type="string">md:gap-x-5</item>
                </item>
            </item>
        </argument>
    </arguments>
</referenceBlock>

AttributeRemove

id:attribute_remove

完全移除属性

示例 catalog_product_view.xml

<referenceBlock name="product.detail.page">
    <arguments>
        <argument name="after_html_actions" xsi:type="array">
            <item name="remove-attribute" xsi:type="array">
                <item name="xpath" xsi:type="string"><![CDATA[(/root/section/div/div)[1]]]></item>
                <item name="action" xsi:type="string">attribute_remove</item>
                <item name="attribute" xsi:type="string">@click</item>
            </item>
        </argument>
    </arguments>
</referenceBlock>

ChildHtml

id:child_html

将子HTML插入输出中的特定部分。这需要您在布局XML中添加子块。

有效目标

  • start:节点内容的开头(在当前子元素之前)
  • end:节点内容的末尾(在当前子元素之后)
  • before:当前节点之前
  • after:当前节点之后

示例 default.xml

<referenceBlock name="footer-content">
    <block name="footer.social_icons" template="Magento_Theme::html/footer/social_icons.phtml"/>
    <arguments>
        <argument name="after_html_actions" xsi:type="array">
            <item name="add-in-social-icons" xsi:type="array">
                <item name="xpath" xsi:type="string"><![CDATA[(/root/div)[1]]]></item>
                <item name="action" xsi:type="string">child_html</item>
                <item name="block" xsi:type="string">footer.social_icons</item>
                <item name="target" xsi:type="string">start</item>
            </item>
        </argument>
    </arguments>
</referenceBlock>

Remove

id:remove

从DOM中移除一个元素。

示例 default.xml

<referenceBlock name="footer-content">
    <block name="footer.social_icons" template="Magento_Theme::html/footer/social_icons.phtml"/>
    <arguments>
        <argument name="after_html_actions" xsi:type="array">
            <item name="zero1-remove-hyva-link" xsi:type="array">
                <item name="xpath" xsi:type="string"><![CDATA[(/root//a[contains(@class, "title-font")])]]></item>
                <item name="action" xsi:type="string">remove</item>
            </item>
        </argument>
    </arguments>
</referenceBlock>

XPath作弊表

  • 选择所有具有'class'为'swatch-option'的标签 /root//label[contains(@class, 'swatch-option')]

其他建议

当涉及到停止块输出内容时,使用布局XML通常可以消除更改模板的需求。注意在引用块时,必须使用它的名称而不是别名。例如,不要这样做

<?= // $block->getChildHtml('review_form') ?>

这样做

<referenceBlock name="product.review.form" display="false"></referenceBlock>

TODO

  • 使调试标志可设置/启用
  • 使hyva在生产模式下友好