MJML integration into Nette Framework.
The recommended way to is via Composer:
composer require surda/mjml-nette
After that you have to register extensions in config.neon:
extensions:
mjml: Surda\Mjml\DI\MjmlExtension
mjml.renderer: Surda\Mjml\DI\MjmlBinaryRendererExtension
List of all configuration options:
mjml:
debug: %debugMode%
tempDir: %tempDir%/cache/latte
templateFactory: \Surda\Mjml\TemplateFactory
mjml.renderer:
renderer: \Surda\Mjml\Renderer\BinaryRenderer
options:
bin: mjml
minify: FALSE
validationLevel: strict
beautify: TRUE
Install MJML
$ npm install -g mjml
extensions:
mjml: Surda\Mjml\DI\MjmlExtension
mjml.renderer: Surda\Mjml\DI\MjmlApiRendererExtension
Minimal configuration:
mjml.renderer:
options:
applicationId: 'application-id'
secretKey: 'secret-key'
List of all configuration options:
mjml:
debug: %debugMode%
tempDir: %tempDir%/cache/latte
templateFactory: \Surda\Mjml\TemplateFactory
mjml.renderer:
renderer: \Surda\Mjml\Renderer\ApiRenderer
options:
applicationId: 'application-id'
secretKey: 'secret-key'
uri: 'https://api.mjml.io/v1/render'
Template template.mjml
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-image width="100px" src="https://mjml.io/assets/img/logo-small.png"></mj-image>
<mj-divider border-color="#F45E43"></mj-divider>
<mj-text font-size="20px" color="#F45E43" font-family="helvetica">Hello {$foo}</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
use Surda\Mjml\MjmlTemplateFactory;
class MailSender
{
/** @var MjmlTemplateFactory */
private $mjmlTemplateFactory;
/**
* @param MjmlTemplateFactory $mjmlTemplateFactory
*/
public function __construct(MjmlTemplateFactory $mjmlTemplateFactory)
{
$this->mjmlTemplateFactory = $mjmlTemplateFactory;
}
public function sendEmail(): void
{
$template = $this->mjmlTemplateFactory->create();
$template->setFile('/path/to/template.mjml');
$template->setParameters(['foo' => 'World']);
$mail = new Message;
$mail->setHtmlBody($template);
// or
$template = $this->mjmlTemplateFactory->create();
$mail = new Message;
$mail->setHtmlBody($template->renderToString('/path/to/template.mjml', ['foo' => 'World'])));
// ...
}
}
Only render *.latte template from *.mjml template
use Surda\Mjml\Engine;
class Convertor
{
/** @var Engine */
private $engine;
/**
* @param Engine $engine
*/
public function __construct(Engine $engine)
{
$this->engine = $engine;
}
public function convert(): void
{
$mjmlFile = '/path/to/template.mjml';
$latteFile = $this->engine->renderLatteFile($mjmlFile);
// or
$mjmlFile = '/path/to/template.mjml';
$latteFile = '/path/to/template.latte';
$this->engine->renderLatteFile($mjmlFile, $latteFile);
}
}
More in the MJML documentation.