discutea / mjml-bundle
MJML 的 Symfony 扩展包
v3.2.0
2019-10-09 11:34 UTC
Requires
- php: ^7.1.0
- symfony/dependency-injection: ^3.4 || ^4.0
- symfony/process: ^3.4 || ^4.0
- symfony/twig-bundle: ^3.4 || ^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.10
- matthiasnoback/symfony-config-test: ^4.0
- phpstan/phpstan: ^0.9.2
- phpunit/phpunit: ^7.4
Suggests
- swiftmailer/swiftmailer: Plugin to integrate MJML in SwiftMailer
README
使用此扩展包将 MJML 3 和 4 与 Symfony >= 3 结合使用。
安装
使用 Symfony Flex 的应用程序
打开命令行,进入您的项目目录,然后执行
$ composer require notfloran/mjml-bundle
不使用 Symfony Flex 的应用程序
步骤 1:下载扩展包
打开命令行,进入您的项目目录,并执行以下命令以下载此扩展包的最新稳定版本
$ composer require notfloran/mjml-bundle
此命令需要您全局安装了 Composer,如 Composer 文档中的安装章节所述。
步骤 2:启用扩展包
然后,将扩展包添加到项目 app/AppKernel.php 文件中注册的扩展包列表中,以启用它
// app/AppKernel.php // ... class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... new NotFloran\MjmlBundle\MjmlBundle(), ]; // ... } // ... }
渲染器
目前只有一个渲染器可用,即二进制渲染器。
二进制
安装 MJML
$ npm install mjml
然后您需要更新配置
# config/packages/mjml.yaml mjml: renderer: binary # default: binary options: binary: '%kernel.project_dir%/node_modules/.bin/mjml' # default: mjml minify: true # default: false validation_level: skip # default: strict. See https://mjml.io/documentation/#validating-mjml
自定义
首先,您必须创建一个实现了 NotFloran\MjmlBundle\Renderer\RendererInterface 的类,然后将其声明为服务。
最后,您必须更改配置
# config/packages/mjml.yaml mjml: renderer: 'service' options: service_id: 'App\Mjml\MyCustomRenderer'
API
该扩展包没有与 MJML API 的官方集成。
您可以通过使用 juanmiguelbesada/mjml-php 并遵循此 gist 来创建自己的集成:https://gist.github.com/notFloran/ea6bab137be628f6a0c19054e08e6906。
用法
使用 "mjml" 服务
{# templates/mail/example.mjml.twig #} <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 {{ name }} from MJML and Symfony </mj-text> </mj-column> </mj-section> </mj-body> </mjml>
$message = (new \Swift_Message('Hello Email')) ->setFrom('my-app@example.fr') ->setTo('me@example.fr') ->setBody( $this->get('mjml')->render( $this->get('twig')->render('templates/mail/example.mjml.twig', [ 'name' => 'Floran' ]) ), 'text/html' ) ; $this->get('mailer')->send($message);
使用 "mjml" twig 标签
{# mail/example.mjml.twig #} {% block email_content %} {% 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 {{ name }} from MJML and Symfony </mj-text> </mj-column> </mj-section> </mj-body> </mjml> {% endmjml %} {% endblock %}
$message = (new \Swift_Message('Hello Email')) ->setFrom('my-app@example.fr') ->setTo('me@example.fr') ->setBody( $this->get('twig')->render('templates/mail/example.mjml.twig', [ 'name' => 'Floran' ]), 'text/html' ) ; $this->get('mailer')->send($message);
SwiftMailer 集成
声明以下服务
NotFloran\MjmlBundle\SwiftMailer\MjmlPlugin: tags: [swiftmailer.default.plugin]
创建一个具有 MJML 主体(不带 {% mjml %})和 text/mjml 作为内容类型的 SwiftMailer 消息
$message = (new \Swift_Message('Hello Email')) ->setFrom('send@example.com') ->setTo('recipient@example.com') ->setBody( $this->renderView('mail/example.mjml.twig'), 'text/mjml' ) $mailer->send($message);
插件将自动渲染 MJML 主体,并用渲染的 HTML 替换主体。
在使用 spool 的情况下:MJML 内容将被保存到 spool 中,并在 spool 清空时渲染。
许可证
MjmlBundle 依据 MIT 许可证 许可。