Skip to content
Merged
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class AppKernel extends Kernel

## Renderer

For the moment only one renderer is available, the binary renderer.
Choose from one of two renderers, the binary renderer and mjml-php.

### Binary

Expand All @@ -82,6 +82,19 @@ mjml:

The `node` option is there for those who have problems with `$PATH`, see [#35](https://github.com/notFloran/mjml-bundle/issues/35).

### mjml-php

Install [mjml-php](https://github.com/alekitto/mjml-php), a PHP extension that embeds [MRML](https://github.com/jdrouet/mrml),
the MJML rendering engine written in Rust, so that you can render MJML templates directly from PHP without shelling out to a CLI tool.

Then you need to update the configuration:

```yaml
# config/packages/mjml.yaml
mjml:
renderer: mjml_php
```

### Custom

First you must create a class which implements `NotFloran\MjmlBundle\Renderer\RendererInterface`, then declare it as a service.
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/MjmlExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NotFloran\MjmlBundle\DependencyInjection;

use NotFloran\MjmlBundle\Renderer\BinaryRenderer;
use NotFloran\MjmlBundle\Renderer\MjmlPhpRenderer;
use NotFloran\MjmlBundle\Renderer\RendererInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -31,6 +32,10 @@ public function load(array $configs, ContainerBuilder $container): void
$rendererDefinition->addArgument($config['options']['mjml_version']);
$container->setDefinition($rendererDefinition->getClass(), $rendererDefinition);
$rendererServiceId = $rendererDefinition->getClass();
} elseif ('mjml_php' === $config['renderer']) {
$rendererDefinition = new Definition(MjmlPhpRenderer::class);
$container->setDefinition($rendererDefinition->getClass(), $rendererDefinition);
$rendererServiceId = $rendererDefinition->getClass();
} elseif ('service' === $config['renderer']) {
$rendererServiceId = $config['options']['service_id'];
} else {
Expand Down
22 changes: 22 additions & 0 deletions src/Renderer/MjmlPhpRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace NotFloran\MjmlBundle\Renderer;

use Mjml\Mjml;
use Mjml\Exception\RenderException;

final class MjmlPhpRenderer implements RendererInterface
{
public function render(string $mjmlContent): string
{
try {
$mjml = new Mjml(['disable_comments' => true]);

$result = $mjml->render($mjmlContent);

return $result->getBody();
} catch (RenderException $e) {
throw new \RuntimeException('MJML rendering exception: '.$e->getMessage(), 0, $e);
}
}
}