Twig Template Engine for Magento2
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Magento2 Twig Template Engine

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.

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.

Event twig_init with event object twig. You can add here more functions, filters, tags, etc.

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="-">
            <argument name="show_part" xsi:type="string">welcome</argument>

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

Access helper methods

Write in your .twig file:

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



Installation via Composer

Add the following to the require section of your Magento 2 composer.json file

"schumacherfm/mage2-twig": "dev-master"

additionally add the following in the repository section

    "repositories": [
        "type": "vcs",
        "url": ""

run composer update

add the following to app/etc/config.php



  • Magento >= 2
  • php >= 5.4.0

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



  • Initial release




Cyrill Schumacher

My pgp public key