Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/qferr/mjml-twig

MJML extension for Twig
https://github.com/qferr/mjml-twig

mjml php twig

Last synced: 5 days ago
JSON representation

MJML extension for Twig

Awesome Lists containing this project

README

        

Twig MJML extension
===================

This package is a Twig extension that provides the following:
* mjml_to_html filter: processes a mjml email template.

```twig
{% apply mjml_to_html %}




Hello {{ username }}


{% endapply %}
```

Because we have two ways for rendering MJML to HML, the extension depends on a renderer:
* **BinaryRenderer**: using the MJML library. You will have to provide the location of the MJML binary. Don’t forget to install it with the Node package manager.
* **ApiRenderer**: using the MJML API. Nothing to install. You will have to provide the credentials to access of the API.

Thanks to the library [MJML in PHP](https://github.com/qferr/mjml-php) for make easier the integration of MJML in PHP.
Read the article [Rendering MJML in PHP](https://medium.com/@qferrer/rendering-mjml-in-php-982d703aa703?source=friends_link&sk=7c5553ae7fcfcdde889bdd3b776c90a9) for more informations.

Installation
------------

`composer require qferr/mjml-twig`

Usage
-----

```php
addExtension(new MjmlExtension($renderer));

$html = $twig->render('newsletter.mjml.twig', [
'username' => 'Quentin'
]);
```

You can now start using MJML in any Twig template.

Integrating in Symfony
----------------------
Register the MJML extension as a service and tag it with `twig.extension`.

```yaml
# config/services.yaml
services:
# Qferrer\Mjml\Http\CurlApi:
# arguments:
# - '%env(MJML_APP_ID)%'
# - '%env(MJML_SECRET_KEY)%'

# mjml_renderer:
# class: Qferrer\Mjml\Renderer\ApiRenderer
# arguments:
# - '@Qferrer\Mjml\Http\CurlApi'

mjml_renderer:
class: Qferrer\Mjml\Renderer\BinaryRenderer
arguments:
- '%kernel.project_dir%/node_modules/.bin/mjml'

Qferrer\Mjml\Twig\MjmlExtension:
arguments: ['@mjml_renderer']
tags: ['twig.extension']
```

Source: [Using MJML with Twig](https://medium.com/@qferrer/using-mjml-with-twig-cccea9af0086?source=friends_link&sk=0271c98a6225ff216cec5faefd6d7267)