Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ $html = $twig->render('newsletter.mjml.twig', [

You can now start using MJML in any Twig template.

## Alternate Possability
This works on some Serversystems a little bit better and on mean time you can debug the different Steps from twig -> mjml -> html

```php
<?php
require_once 'vendor/autoload.php';

use \Qferrer\Mjml\Renderer\ApiRenderer;
use \Qferrer\Mjml\Renderer\BinaryRenderer;
use \Qferrer\Mjml\Twig\MjmlExtension;

$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');
$twig = new \Twig\Environment($loader);


$tmpRenderFile = __DIR__ . "/templates/newsletter.mjml"; //Where the twig result will get parsed into
$renderer = new \Qferrer\Mjml\Renderer\BinaryRenderer(__DIR__ . '/node_modules/.bin/mjml', $tmpRenderFile);
$twig->addExtension(new MjmlExtension($renderer, $tmpRenderFile));

//returns the content of __DIR__ . "/templates/newsletter.mjml.html created by the new BinaryRenderer PR https://github.com/qferr/mjml-php/pull/25
$html = $twig->render('newsletter.mjml.twig', [
'username' => 'Quentin'
]);

Integrating in Symfony
----------------------
Register the MJML extension as a service and tag it with `twig.extension`.
Expand Down
19 changes: 18 additions & 1 deletion src/Mjml/Twig/MjmlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ class MjmlExtension extends AbstractExtension
*/
protected $renderer;

/**
* @var string
*/
private $mjmlFilePath;

/**
* MjmlExtension constructor.
*
* @param RendererInterface $renderer
* @param string $mjmlFilePath if set, reads this mjml file and creates a mjml.html file which will be read and return on render
*/
public function __construct(RendererInterface $renderer)
public function __construct(RendererInterface $renderer, string $mjmlFilePath = "")
{
$this->renderer = $renderer;
$this->mjmlFilePath = $mjmlFilePath;
}

/**
Expand All @@ -45,6 +52,16 @@ public function getFilters(): array
*/
public function render(string $content): string
{
if ($this->mjmlFilePath !== '') {
$this->writeInFile($content);
}
return $this->renderer->render($content);
}

private function writeInFile(string $content)
{
$myfile = fopen($this->mjmlFilePath, "w") or die("Unable to open file!");
fwrite($myfile, $content);
fclose($myfile);
}
}