Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Mjml Bundle Laravel Package

discutea/mjml-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require notfloran/mjml-bundle
    

    For Symfony Flex projects, this auto-registers the bundle. For non-Flex projects, manually add new NotFloran\MjmlBundle\MjmlBundle() to AppKernel.php.

  2. First Use Case: Render an MJML template via the binary renderer:

    use NotFloran\MjmlBundle\Renderer\BinaryRenderer;
    
    $renderer = new BinaryRenderer();
    $html = $renderer->render('<mjml><mj-body><mj-section><mj-column><mj-text>Hello World!</mj-text></mj-column></mj-section></mj-body></mjml>');
    
  3. Where to Look First:

    • Configuration: Check config/packages/notfloran_mjml.yaml for default settings (e.g., MJML binary path, version).
    • Renderer: Focus on BinaryRenderer for direct usage or extend it for custom logic.
    • Twig Integration: If using Twig, explore the MjmlExtension for seamless template rendering.

Implementation Patterns

Core Workflows

  1. Direct Rendering: Use the BinaryRenderer for one-off MJML-to-HTML conversions:

    $renderer = $container->get('notfloran_mjml.renderer.binary');
    $html = $renderer->render($mjmlString);
    
  2. Twig Integration: Extend Twig templates with MJML syntax:

    {% mjml %}
        <mjml>
            <mj-body>
                <mj-section>
                    <mj-column>
                        <mj-text>{{ message }}</mj-text>
                    </mj-column>
                </mj-section>
            </mj-body>
        </mjml>
    {% endmjml %}
    

    Render via:

    $html = $twig->render('path/to/template.twig');
    
  3. Service Integration: Inject the renderer into services for reusable logic:

    class EmailService {
        public function __construct(private BinaryRenderer $renderer) {}
    
        public function generateEmailTemplate(string $content): string {
            return $this->renderer->render($content);
        }
    }
    
  4. Configuration Overrides: Customize MJML binary path or version in config/packages/notfloran_mjml.yaml:

    notfloran_mjml:
        binary_path: '/custom/path/to/mjml'
        version: '4.14.1'
    

Integration Tips

  • Mailers: Pair with Symfony Mailer for email templates:
    $email = (new Email())
        ->html($renderer->render($mjmlTemplate))
        ->subject('Your MJML Email');
    $mailer->send($email);
    
  • Asset Management: Use Symfony’s asset system to reference MJML partials or components.
  • Testing: Mock BinaryRenderer in tests to avoid dependency on the MJML binary:
    $this->mockBuilder()
         ->disableOriginalConstructor()
         ->getMock()
         ->method('render')
         ->willReturn('<html>Mocked HTML</html>');
    

Gotchas and Tips

Pitfalls

  1. Binary Dependencies:

    • The bundle relies on the MJML CLI. Ensure it’s installed globally or specify a custom path in config.
    • Debugging: If rendering fails, verify the binary path and version compatibility (e.g., MJML v4 requires Node.js 12+).
  2. Twig Caching:

    • MJML templates rendered via Twig may not cache efficiently. Use {% mjml autoescape=false %} to avoid double-escaping issues.
  3. Configuration Overrides:

    • Changes to notfloran_mjml.yaml require a cache clear:
      php bin/console cache:clear
      
  4. Version Mismatches:

    • The bundle supports MJML 3 and 4. Ensure your composer.json and config align (e.g., version: '4' in config for MJML 4).

Debugging Tips

  • Logs: Enable Symfony’s debug mode to log MJML binary execution errors.
  • Output Validation: Use tools like Email on Acid to validate rendered HTML.
  • Docker: For CI/CD, include MJML installation in your Dockerfile:
    RUN npm install -g mjml@4
    

Extension Points

  1. Custom Renderers: Extend BinaryRenderer to add pre/post-processing:

    class CustomRenderer extends BinaryRenderer {
        public function render(string $mjml): string {
            $html = parent::render($mjml);
            return $this->postProcess($html);
        }
    }
    

    Register as a service:

    services:
        NotFloran\MjmlBundle\Renderer\CustomRenderer: ~
    
  2. Twig Filters: Add MJML-specific Twig filters (e.g., for dynamic MJML generation):

    $twig->addFilter(new \Twig\TwigFilter('mjml_component', [$renderer, 'renderComponent']));
    
  3. Event Listeners: Hook into MJML rendering events (e.g., to modify templates before conversion):

    $eventDispatcher->addListener(
        'notfloran_mjml.render',
        [$this, 'onMjmlRender']
    );
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui