Skip to content

Commit

Permalink
feat(#9)!: post process blade views with mjml (#10)
Browse files Browse the repository at this point in the history
* feat(#9)!: post process blade views with mjml

Views are now post-processed after blade templating

! BE SURE THAT YOUR VIEWS FINISH WITH .mjml.blade.php !

* Fix styling

---------

Co-authored-by: EvanSchleret <EvanSchleret@users.noreply.github.com>
  • Loading branch information
EvanSchleret and EvanSchleret authored Jan 5, 2025
1 parent a400782 commit f0d7070
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ composer require evanschleret/lara-mjml

You can use this package by simply calling the `view` helper function with the name of your mjml template.

Your views must be names as `*.mjml.blade.php` to be compiled by this package.

### Environment Variables and Configuration

You can set the path to the mjml binary in your `.env` file.
Expand Down
20 changes: 6 additions & 14 deletions src/Providers/LaraMjmlServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace EvanSchleret\LaraMjml\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Config;
use EvanSchleret\LaraMjml\Views\Engines\MJMLEngine;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Spatie\Mjml\Mjml;

class LaraMjmlServiceProvider extends ServiceProvider
{
Expand All @@ -26,18 +25,11 @@ public function boot(): void
__DIR__.'/../../config/laramjml.php' => config_path('laramjml.php'),
]);

Blade::extend(function ($view) {
if (stripos($view, '<mjml>') !== false) {
return Mjml::new()
->beautify(Config::get('laramjml.beautify'))
->minify(Config::get('laramjml.minify'))
->keepComments(Config::get('laramjml.keep_comments'))
->convert($view, ...Config::get('laramjml.options'))
->html();
}

return $view;
View::getEngineResolver()->register('mjml', function () {
return new MJMLEngine;
});

View::addExtension('mjml.blade.php', 'mjml');
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Views/Engines/MJMLEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace EvanSchleret\LaraMjml\Views\Engines;

use Illuminate\Contracts\View\Engine;
use Illuminate\Support\Facades\Blade;
use Spatie\Mjml\Mjml;

class MJMLEngine implements Engine
{
public function get($path, array $data = [])
{
// Compile la vue Blade en HTML
$viewContent = file_get_contents($path);
$compiledView = Blade::render($viewContent, $data);

// Post-process avec MJML
return Mjml::new()
->beautify(config('laramjml.beautify'))
->minify(config('laramjml.minify'))
->keepComments(config('laramjml.keep_comments'))
->convert($compiledView, ...config('laramjml.options'))
->html();
}
}

0 comments on commit f0d7070

Please sign in to comment.