2017 © Pedro Peláez
 

magento2-module magento-twig

Twig template engine for Magento 2

image

schumacherfm/magento-twig

Twig template engine for Magento 2

  • Friday, June 8, 2018
  • by SchumacherFM
  • Repository
  • 4 Watchers
  • 38 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 13 Forks
  • 1 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Magento 2 Twig Template Engine

Twig template engine for Magento2., (*1)

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

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., (*3)

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., (*4)

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, (*5)

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

Configuration options can be found Stores -> Settings -> Configuration -> Advanced -> Developer -> Twig., (*7)

Frontend Integration

Your template files must have the file extension .twig to get automatically recognized., (*8)

In your layout xml files or blocks please specify the new template, (*9)

<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:, (*10)

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

Tests

@todo, (*11)

Support / Contribution

Report a bug using the issue tracker or send us a pull request., (*12)

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

I am using that model: A successful Git branching model, (*14)

For versioning have a look at Semantic Versioning 2.0.0, (*15)

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, (*16)

Author

Cyrill Schumacher, (*17)

My pgp public key, (*18)

The Versions

08/06 2018

dev-master

9999999-dev

Twig template engine for Magento 2

  Sources   Download

OSL-3.0

The Requires

  • php ~5.6.0|7.0.2|7.0.4|~7.0.6|~7.1.0
  • magento/framework >=100.0.0
  • magento/module-backend >=100.0.0
  • magento/module-config >=100.0.0
  • twig/twig ~2.0

 

08/06 2018

1.1.2

1.1.2.0

Twig template engine for Magento 2

  Sources   Download

OSL-3.0

The Requires

  • php ~5.6.0|7.0.2|7.0.4|~7.0.6|~7.1.0
  • magento/framework >=100.0.0
  • magento/module-backend >=100.0.0
  • magento/module-config >=100.0.0
  • twig/twig ~2.0

 

08/06 2018

1.1.1

1.1.1.0

Twig template engine for Magento 2

  Sources   Download

OSL-3

The Requires

  • php ~5.6.0|7.0.2|7.0.4|~7.0.6|~7.1.0
  • magento/framework >=100.0.0
  • magento/module-backend >=100.0.0
  • magento/module-config >=100.0.0
  • twig/twig ~2.0