Skip to content

Commit

Permalink
chore: prepare for craft 5 (#24)
Browse files Browse the repository at this point in the history
* chore: throw exception if node/mjml not found

fixes #20

Signed-off-by: Fred Carlsen <[email protected]>

* fix: makes sure parsing will always use the site template path

fixes #21

Signed-off-by: Fred Carlsen <[email protected]>

* chore: typos

Signed-off-by: Fred Carlsen <[email protected]>

---------

Signed-off-by: Fred Carlsen <[email protected]>
  • Loading branch information
sjelfull authored May 27, 2024
1 parent 27610cd commit 00db9a9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 2.0.2 - 2024-05-27
### Fixed
- Fixed issue where using the plugin when rendering a template with the Craft Core Mailer would use the CP templates folder as relative path for `mj-include` calls

## 2.0.1 - 2024-05-26
### Fixed
- Fixed issues with saving settings
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ return [

## Using MJML

You can either use the MJML cli locally, or the MJML API.
You can either use the MJML CLI locally, or the MJML API.

To use the cli, you need to install both Node and [the mjml executable](https://mjml.io/documentation/#installation):
To use the CLI, you need to install both Node and [the mjml executable](https://mjml.io/documentation/#installation):

To use the API, you need to [request a API key](https://mjml.io/api).

Expand Down
9 changes: 0 additions & 9 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,9 @@ class Settings extends Model
public string $nodePath = '';
public string $mjmlCliPath = '';
public string $mjmlCliConfigArgs = '';
public string $mjmlCliIncludesPath = '';
public string $appId = '';
public string $secretKey = '';

public function init(): void
{
$this->mjmlCliIncludesPath = Craft::$app->getView()->getTemplatesPath();

parent::init();
}

public function defineBehaviors(): array
{
return [
Expand All @@ -45,7 +37,6 @@ public function defineBehaviors(): array
'nodePath',
'mjmlCliPath',
'mjmlCliConfigArgs',
'mjmlCliIncludesPath',
'appId',
'secretKey',
],
Expand Down
45 changes: 37 additions & 8 deletions src/services/MJMLService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
use superbig\mjml\exceptions\MJMLException;
use superbig\mjml\MJML;
use superbig\mjml\models\MJMLModel;
use Twig\Error\LoaderError;
use Twig\Error\SyntaxError;
use yii\base\ErrorException;
use yii\base\Exception;
use yii\base\InvalidConfigException;

/**
* @author Superbig
Expand Down Expand Up @@ -69,10 +72,20 @@ public function parse(string $html): ?MJMLModel
}
}

public function include(string $template = '', $variables = [], $renderMethod = 'cli')
/**
* @param string $template
* @param array $variables
* @param string $renderMethod
* @return string|null
* @throws LoaderError
* @throws SyntaxError
*/
public function include(string $template = '', array $variables = [], string $renderMethod = 'cli'): ?string
{
$view = Craft::$app->getView();

try {
$templatePath = Craft::$app->getView()->resolveTemplate($template, View::TEMPLATE_MODE_SITE);
$templatePath = $view->resolveTemplate($template, View::TEMPLATE_MODE_SITE);

if (!$templatePath) {
throw new MJMLException('Could not find template: ' . $template);
Expand All @@ -95,9 +108,11 @@ public function include(string $template = '', $variables = [], $renderMethod =
throw new MJMLException('Could not render template: ' . $template);
}

return Craft::$app->getView()->renderString($output->output(), $variables);
return $view->renderString($output->output(), $variables);
} catch (MJMLException $e) {
Craft::error('Could not generate output: ' . $e->getMessage(), 'mjml');

return null;
}
}

Expand All @@ -110,21 +125,35 @@ public function include(string $template = '', $variables = [], $renderMethod =
*/
public function parseCli(?string $html = null): ?MJMLModel
{
$view = Craft::$app->getView();
$oldTemplateMode = $view->getTemplateMode();
$view->setTemplateMode(View::TEMPLATE_MODE_SITE);
$templatesPath = $view->getTemplatesPath();

$settings = MJML::$plugin->getSettings();
$configArgs = App::parseEnv($settings->mjmlCliConfigArgs);

$mjmlCliIncludesPath = App::parseEnv($settings->mjmlCliIncludesPath);
if (!empty($mjmlCliIncludesPath)) {
$configArgs = "{$configArgs} --config.filePath {$mjmlCliIncludesPath}";
}
$configArgs = "{$configArgs} --config.filePath {$templatesPath}";

$nodePath = App::parseEnv($settings->nodePath);
$mjmlCliPath = App::parseEnv($settings->mjmlCliPath);
$mjmlPath = "{$nodePath} {$mjmlCliPath}";

$hash = md5($html);
$tempPath = Craft::$app->getPath()->getTempPath() . "/mjml/mjml-{$hash}.html";
$tempOutputPath = Craft::$app->getPath()->getTempPath() . "/mjml/mjml-output-{$hash}.html";

$view->setTemplateMode($oldTemplateMode);

// Check if Node.js exists
if (!file_exists($nodePath)) {
throw new InvalidConfigException("Node.js executable not found at path: {$nodePath}");
}

// Check if MJML CLI exists
if (!file_exists($mjmlCliPath)) {
throw new InvalidConfigException("MJML CLI executable not found at path: {$mjmlCliPath}");
}

try {
if (!file_exists($tempOutputPath)) {
FileHelper::writeToFile($tempPath, $html);
Expand Down
2 changes: 1 addition & 1 deletion src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
}}

{{ forms.autosuggestField({
label: 'MJML CLI PAth',
label: 'MJML CLI Path',
instructions: 'Enter the path to where the MJML CLI installed with npm is located, i.e. `/usr/local/bin/mjml`',
id: 'mjmlCliPath',
name: 'mjmlCliPath',
Expand Down

0 comments on commit 00db9a9

Please sign in to comment.