An open API service indexing awesome lists of open source software.

https://github.com/schumacherfm/magento2-twig

Twig Template Engine for Magento2
https://github.com/schumacherfm/magento2-twig

magento magento2 php template-engine twig twig-template-engine

Last synced: 3 months ago
JSON representation

Twig Template Engine for Magento2

Awesome Lists containing this project

README

        

Magento 2 Twig Template Engine
============

[Twig](http://twig.sensiolabs.org) 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
```

2. Enable the module
```
bin/magento module:enable SchumacherFM_Twig
```

3. 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](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](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

```xml



welcome

```

#### Example header.phtml converted to header.twig

```php
getShowPart()):
case 'welcome': ?>

  • getWelcome() ?>



  • getChildHtml(); ?>

    ```

    ```twig
    {% if getShowPart() == 'welcome' %}

  • {{ getWelcome() }}

  • {% endif %}

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

    #### Example breadcrumbs.phtml converted to breadcrumbs.twig

    ```php

    ```

    ```twig
    {% if crumbs %}


    {% endif %}
    ```

    #### Access helper methods

    Write in your `.twig` file:

    ```twig
    {{ 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](http://nvie.com/posts/a-successful-git-branching-model/)

    For versioning have a look at [Semantic Versioning 2.0.0](http://semver.org/)

    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](http://cyrillschumacher.com)

    [My pgp public key](http://www.schumacher.fm/cyrill.asc)