Install Dependencies:
npm install -g mjml # Install MJML CLI globally via Node.js
composer require assoconnect/mjml-bundle
Configure the Bundle:
Add to config/bundles.php:
Assoconnect\MJMLBundle\AssoconnectMJMLBundle::class => ['all' => true],
Create an MJML Template:
Save a file like templates/emails/welcome.mjml.twig:
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-text>Hello {{ name }}!</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
Trigger Cache Warmup:
php bin/console cache:warmup --env=prod
This compiles MJML to HTML and stores it in var/cache/prod/.
Use in Twig:
{% render 'emails/welcome.html.twig', { name: 'John' } %}
templates/emails/newsletter.mjml.twig with dynamic content.$html = $this->get('twig')->render('emails/newsletter.html.twig', [
'user' => $user,
'products' => $products,
]);
Write MJML in Twig:
Use .mjml.twig files for MJML syntax with embedded Twig logic.
Example:
<mjml>
<mj-section background-color="{{ bg_color }}">
<mj-column>
<mj-text font-size="20px">{{ headline }}</mj-text>
</mj-column>
</mj-section>
</mjml>
Cache Warmup:
Run cache:warmup to compile MJML to HTML. The bundle generates .html.twig files in var/cache/.
Reuse Compiled Templates:
Render the compiled .html.twig files with dynamic data:
{% render 'emails/compiled/newsletter.html.twig', {
headline: 'Summer Sale!',
bg_color: '#FF5733'
} %}
Custom Tags:
Define reusable MJML components in Twig includes (e.g., templates/_components/header.mjml.twig) and include them:
{% include '_components/header.mjml.twig' %}
Environment-Specific Config:
Override MJML CLI path in config/packages/assoconnect_mjml.yaml:
assoconnect_mjml:
mjml_binary: '/usr/local/bin/mjml-custom'
Debugging Compilation:
Use --verbose flag in cache:warmup to inspect MJML compilation logs.
CI/CD Pipeline: Add cache warming to deployment scripts to ensure templates are pre-compiled.
Deprecated Bundle:
spatie/laravel-mjml (Laravel).Process component.Cache Invalidation:
cache:clear) if MJML templates are updated but changes aren’t reflected..html.twig files directly in var/cache/—they are auto-generated.MJML CLI Dependency:
mjml) are installed globally. Test with:
mjml --version
Twig Expressions in MJML:
{{ if }}, {% for %}) won’t resolve during compilation. Only use static values or variables passed at render time:
<!-- ❌ Won't work (Twig logic) -->
{% if promo %}
<mj-text>Promo!</mj-text>
{% endif %}
<!-- ✅ Works (static or render-time value) -->
<mj-text>{{ promo_message }}</mj-text>
File Naming:
*.mjml.twig. The bundle ignores other extensions.Compilation Errors:
var/log/dev.log for MJML CLI output. Errors may appear as Symfony exceptions.Missing Templates:
templates/emails/) and the file exists. The bundle throws a CommandNotFoundException if the template is missing.Performance:
Custom MJML CLI Arguments: Override default MJML CLI flags in config:
assoconnect_mjml:
mjml_args: ['--minify', '--no-hide-attributes']
Post-Compilation Processing:
Extend the bundle by listening to the mjml.compile event (if supported) to modify compiled HTML.
Alternative Storage:
Change cache directory in config/packages/assoconnect_mjml.yaml:
assoconnect_mjml:
cache_dir: '%kernel.project_dir%/var/mjml_cache'
How can I help you explore Laravel packages today?