定制 schumacherfm/magento-twig 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

schumacherfm/magento-twig

Composer 安装命令:

composer require schumacherfm/magento-twig

包简介

Twig template engine for Magento 2

README 文档

README

Twig template engine for Magento2.

This template engine is meant to be used additionally to the .phtml files and does not provide any .twig template file.

A use case would be if you write your first Magento2 module you can require this package and write all your template files in Twig.

Installation

  1. Add dependency
composer require schumacherfm/magento-twig
  1. Enable the module
bin/magento module:enable SchumacherFM_Twig
  1. Update the database entries
bin/magento setup:upgrade

Events & Configuration

The Twig template engine class dispatches two events so that you can modify Twig.

Event twig_loader with event object loader. You can set loader any other class which implements Twig_LoaderInterface. http://twig.sensiolabs.org/doc/api.html#loaders

Event twig_init with event object twig. You can add here more functions, filters, tags, etc. http://twig.sensiolabs.org/doc/advanced.html

Configuration options can be found Stores -> Settings -> Configuration -> Advanced -> Developer -> Twig.

Frontend Integration

Your template files must have the file extension .twig to get automatically recognized.

In your layout xml files or blocks please specify the new template

<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>

Example header.phtml converted to 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 %}

Example breadcrumbs.phtml converted to 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 %}

Access helper methods

Write in your .twig file:

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

Tests

@todo

Support / Contribution

Report a bug using the issue tracker or send us a pull request.

Instead of forking I can add you as a Collaborator IF you really intend to develop on this module. Just ask :-)

I am using that model: A successful Git branching model

For versioning have a look at Semantic Versioning 2.0.0

History

2.0.0

  • Added Magento 2.4.0 compatibility
  • Removed helper functions from app/functions.php since the file is no longer available in Magento 2.4
  • Removed deprecated function layoutBlock from twig environment
  • Updated to twig to 3.0.*

Compatibility

  • Magento >= 2
  • php >= 5.4.0

License

OSL-30

Author

Cyrill Schumacher

My pgp public key

schumacherfm/magento-twig 适用场景与选型建议

schumacherfm/magento-twig 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.71k 次下载、GitHub Stars 达 60, 最近一次更新时间为 2018 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 schumacherfm/magento-twig 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 schumacherfm/magento-twig 我们能提供哪些服务?
定制开发 / 二次开发

基于 schumacherfm/magento-twig 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 14.71k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 60
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 60
  • Watchers: 5
  • Forks: 20
  • 开发语言: PHP

其他信息

  • 授权协议: OSL-3.0
  • 更新时间: 2018-06-08