Skip to content

Commit

Permalink
Refactor Email Templates to treat /templates folder as base folder wh…
Browse files Browse the repository at this point in the history
…en rendering

- Adds getTemplateRoot to barrelstrength\sproutbaseemail\base\EmailTemplates
- Updates Basic Email Templates to implement getTemplateRoot and new getPath behavior
- Updates Custom Email Templates to implement getTemplateRoot and new getPath behavior

https://github.com/barrelstrength/craft-sprout-email/issues/111
https://github.com/barrelstrength/craft-sprout-email/issues/122
  • Loading branch information
BenParizek committed Apr 8, 2020
1 parent e538d9d commit 5ef759f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/base/EmailElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function getEmailTemplates()
$emailTemplates = new $settings->emailTemplateId();
} else {
// custom folder on site path
$templatePath = $sitePath.DIRECTORY_SEPARATOR.$settings->emailTemplateId;
$templatePath = $settings->emailTemplateId;

$emailTemplates = new CustomTemplates();
$emailTemplates->setPath($templatePath);
Expand All @@ -222,7 +222,7 @@ public function getEmailTemplates()

if ($isCustom) {
// custom folder on site path
$templatePath = $sitePath.DIRECTORY_SEPARATOR.$emailTemplateId;
$templatePath = $emailTemplateId;

$emailTemplates = new CustomTemplates();
$emailTemplates->setPath($templatePath);
Expand Down
23 changes: 16 additions & 7 deletions src/base/EmailTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace barrelstrength\sproutbaseemail\base;

use barrelstrength\sproutbaseemail\emailtemplates\CustomTemplates;
use Craft;
use craft\web\View;
use League\HTMLToMarkdown\HtmlConverter;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
Expand Down Expand Up @@ -37,9 +39,16 @@ abstract class EmailTemplates
abstract public function getName(): string;

/**
* The folder path where your email templates exist
* The root folder where the Email Templates exist
*
* This value should be a folder. Sprout Email will look for a required email.html file and an optional email.txt file within this folder.
* @return string
*/
abstract public function getTemplateRoot(): string;

/**
* The folder path where your email templates exist in relation to the folder defined in [[self::getTemplateRoot]]
*
* This value should also be a folder. Sprout Email will look for a required email.twig file and an optional email.txt file within this folder.
*
* @return string
*/
Expand Down Expand Up @@ -103,15 +112,15 @@ protected function processEmailTemplates()
{
$view = Craft::$app->getView();
$oldTemplatePath = $view->getTemplatesPath();
$view->setTemplatesPath($this->getPath());

// @todo - make dynamic
$htmlEmailTemplate = 'email.html';
$textEmailTemplate = 'email.txt';
$view->setTemplatesPath($this->getTemplateRoot());

$htmlEmailTemplate = null;
$textEmailTemplate = $this->getPath().'/email.txt';

// Allow other extensions for email.html
foreach (Craft::$app->getConfig()->getGeneral()->defaultTemplateExtensions as $extension) {
$templateName = 'email.'.$extension;
$templateName = $this->getPath().'/email.'.$extension;

if (Craft::$app->getView()->doesTemplateExist($templateName)) {
$htmlEmailTemplate = $templateName;
Expand Down
7 changes: 6 additions & 1 deletion src/emailtemplates/BasicTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ public function getName(): string
return Craft::t('sprout-base-email', 'Basic Notification (Sprout Email)');
}

public function getTemplateRoot(): string
{
return Craft::getAlias('@sproutbaseemail/templates');
}

/**
* @return string
*/
public function getPath(): string
{
return Craft::getAlias('@sproutbaseemail/templates/_components/emailtemplates/basic');
return '_components/emailtemplates/basic';
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/emailtemplates/CustomTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use barrelstrength\sproutbaseemail\base\EmailTemplates;
use Craft;
use craft\web\View;

/**
* The Custom Templates is used to dynamically create an EmailTemplate
Expand All @@ -28,6 +29,11 @@ public function getName(): string
return Craft::t('sprout-base-email', 'Custom Templates');
}

public function getTemplateRoot(): string
{
return Craft::$app->getView()->getTemplatesPath();
}

/**
* @return string
*/
Expand Down

0 comments on commit 5ef759f

Please sign in to comment.