Skip to content

Commit

Permalink
fix: makes sure parsing will always use the site template path
Browse files Browse the repository at this point in the history
fixes #21

Signed-off-by: Fred Carlsen <[email protected]>
  • Loading branch information
sjelfull committed May 27, 2024
1 parent d5705c7 commit 6ebf141
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 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
10 changes: 1 addition & 9 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use craft\base\Model;
use craft\behaviors\EnvAttributeParserBehavior;
use craft\web\View;

/**
* @author Superbig
Expand All @@ -25,17 +26,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 +38,6 @@ public function defineBehaviors(): array
'nodePath',
'mjmlCliPath',
'mjmlCliConfigArgs',
'mjmlCliIncludesPath',
'appId',
'secretKey',
],
Expand Down
34 changes: 26 additions & 8 deletions src/services/MJMLService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
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;
Expand Down Expand Up @@ -70,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 @@ -96,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 @@ -111,21 +125,25 @@ 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}");
Expand Down

0 comments on commit 6ebf141

Please sign in to comment.