schumacherfm / magento-twig

Magento 2 的 Twig 模板引擎

安装数: 11,394

依赖项: 0

建议者: 0

安全: 0

星标: 61

关注者: 8

分支: 21

开放性问题: 1

类型:magento2-module

2.1.5 2023-04-19 14:03 UTC

This package is auto-updated.

Last update: 2024-09-19 17:03:49 UTC


README

为 Magento2 提供的 Twig 模板引擎。

这个模板引擎旨在作为 .phtml 文件之外的额外使用,并且不提供任何 .twig 模板文件。

一个使用案例是如果您正在编写您的第一个 Magento2 模块,您可以使用此包并将所有模板文件编写为 Twig。

安装

  1. 添加依赖项
composer require schumacherfm/magento-twig
  1. 启用模块
bin/magento module:enable SchumacherFM_Twig
  1. 更新数据库条目
bin/magento setup:upgrade

事件 & 配置

Twig 模板引擎类触发两个事件,以便您可以修改 Twig。

事件 twig_loader,事件对象为 loader。您可以设置 loader 为任何实现了 Twig_LoaderInterface 的其他类。 http://twig.sensiolabs.org/doc/api.html#loaders

事件 twig_init,事件对象为 twig。您可以在其中添加更多函数、过滤器、标签等。 http://twig.sensiolabs.org/doc/advanced.html

配置选项可以在“商店 -> 设置 -> 配置 -> 高级 -> 开发者 -> Twig”下找到。

前端集成

您的模板文件必须具有 .twig 文件扩展名才能自动识别。

请在您的布局 xml 文件或块中指定新的模板

<referenceBlock name="top.links">
    <block class="Magento\Theme\Block\Html\Header" template="html/header.twig" name="header" as="header" before="-">
        <arguments>
            <argument name="show_part" xsi:type="string">welcome</argument>
        </arguments>
    </block>
</referenceBlock>

示例:将 header.phtml 转换为 header.twig

<?php switch ($this->getShowPart()):
    case 'welcome': ?>
        <li class="greet welcome"><?php echo $this->getWelcome() ?></li>
    <?php break; ?>
    <?php case 'other': ?>
        <?php echo $this->getChildHtml(); ?>
    <?php break; ?>
<?php endswitch; ?>
{% if getShowPart() == 'welcome' %}
    <li class="greet welcome">{{ getWelcome() }}</li>
{% endif %}

{% if getShowPart() == 'other' %}
    {{ getChildHtml()|raw }}
{% endif %}

示例:将 breadcrumbs.phtml 转换为 breadcrumbs.twig

<?php if ($crumbs && is_array($crumbs)) : ?>
<div class="breadcrumbs">
    <ul class="items">
        <?php foreach ($crumbs as $crumbName => $crumbInfo) : ?>
            <li class="item <?php echo $crumbName ?>">
            <?php if ($crumbInfo['link']) : ?>
                <a href="<?php echo $crumbInfo['link'] ?>" title="<?php echo $this->escapeHtml($crumbInfo['title']) ?>">
                    <?php echo $this->escapeHtml($crumbInfo['label']) ?>
                </a>
            <?php elseif ($crumbInfo['last']) : ?>
                <strong><?php echo $this->escapeHtml($crumbInfo['label']) ?></strong>
            <?php else: ?>
                <?php echo $this->escapeHtml($crumbInfo['label']) ?>
            <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
</div>
<?php endif; ?>
{% if crumbs %}
<div class="breadcrumbs">
    <ul class="items">
    {% for crumbName,crumbInfo in crumbs %}
        <li class="item {{ crumbName }}">
            {% if crumbInfo.link %}
                <a href="{{ crumbInfo.link }}" title="{{ crumbInfo.title }}">
                    {{ crumbInfo.label }}
                </a>
            {% elseif crumbInfo.last %}
                <strong>{{ crumbInfo.label }}</strong>
            {% else %}
                {{ crumbInfo.label }}
            {% endif %}
        </li>
    {% endfor %}
    </ul>
</div>
{% endif %}

访问助手方法

在您的 .twig 文件中编写

{{ helper("Magento\\Core\\Helper\\Url").getHomeUrl() }}

测试

@todo

支持 / 贡献

使用问题跟踪器报告错误或向我们发送拉取请求。

如果您真的打算开发此模块,我可以将您添加为协作者,而不是分支。请随时询问 :-)

我使用的模型:成功的 Git 分支模型

有关版本控制,请参阅 语义版本控制 2.0.0

历史

2.0.0

  • 添加了 Magento 2.4.0 兼容性
  • 由于文件在 Magento 2.4 中不再可用,从 app/functions.php 中删除了辅助函数
  • 从 twig 环境中删除了已弃用的函数 layoutBlock
  • 更新到 twig 3.0.*

兼容性

  • Magento >= 2
  • php >= 5.4.0

许可证

OSL-30

作者

Cyrill Schumacher

我的 pgp 公钥