yireo/magento2-additional-block-template

N/A

安装次数: 2,584

依赖者: 0

建议者: 0

安全性: 0

星标: 8

关注者: 3

分支: 1

类型:magento2-module

0.0.3 2024-02-03 08:33 UTC

This package is auto-updated.

Last update: 2024-09-03 09:38:33 UTC


README

一个Magento 2模块,允许渲染除原始模板之外的额外模板。

概述

当Magento前端渲染其店面时,它使用XML布局来定位所有块。每个块都可以通过模板来渲染,默认情况下,这意味着一个块只有一个模板。在原始模板之前添加一个新的模板(带有新的<block/>定义),会导致创建另一个$block对象。此模块克服了这一缺点,并允许您向单个$block实例添加多个模板。

安装

composer require yireo/magento2-additional-block-template
bin/magento module:enable Yireo_AdditionalBlockTemplate

用法

使用XML布局,您可以引用特定的块,并在原始模板之前(before)、之后(after)或在原始模板的最终关闭标签之前(nest)添加您自己的模板。

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="category.description">
            <arguments>
                <argument name="additional_templates" xsi:type="array">
                    <item name="example1" xsi:type="array">
                        <item name="template" xsi:type="string">Yireo_Example::example-before.phtml</item>
                        <item name="position" xsi:type="string">before</item>
                    </item>
                    <item name="example2" xsi:type="array">
                        <item name="template" xsi:type="string">Yireo_Example::example-after.phtml</item>
                        <item name="position" xsi:type="string">after</item>
                    </item>
                    <item name="example3" xsi:type="array">
                        <item name="template" xsi:type="string">Yireo_Example::example-nest.phtml</item>
                        <item name="position" xsi:type="string">nest</item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

每个项目名称(在本例中为example1等)都可以在块模板中使用$block->getAdditionalTemplateName()获取。