diff --git a/modules/backend/views/mail/invite.htm b/modules/backend/views/mail/invite.htm index 31745866e..71f218f30 100644 --- a/modules/backend/views/mail/invite.htm +++ b/modules/backend/views/mail/invite.htm @@ -6,15 +6,15 @@ Hi {{ name }} A user account has been created for you on **{{ appName }}**. -{% component 'panel' %} +{% partial 'panel' body %} - Login: `{{ login ?: 'sample' }}` - Password: `{{ password ?: '********' }}` -{% endcomponent %} +{% endpartial %} You can use the following link to sign in: -{% component 'button' url=link %} +{% partial 'button' url=link body %} Sign in to admin area -{% endcomponent %} +{% endpartial %} After signing in you should change your password by clicking your name on the top right corner of the administration area. diff --git a/modules/backend/views/mail/restore.htm b/modules/backend/views/mail/restore.htm index 916d98617..1aa3be13d 100644 --- a/modules/backend/views/mail/restore.htm +++ b/modules/backend/views/mail/restore.htm @@ -8,6 +8,6 @@ Somebody has requested a password reset for your account, if this was not you, p You can use the following link to restore your password: -{% component 'button' url=link type='positive' %} +{% partial 'button' url=link type='positive' body %} Restore password -{% endcomponent %} +{% endpartial %} diff --git a/modules/cms/twig/ComponentTokenParser.php b/modules/cms/twig/ComponentTokenParser.php index 9258a38bb..450f98860 100644 --- a/modules/cms/twig/ComponentTokenParser.php +++ b/modules/cms/twig/ComponentTokenParser.php @@ -49,7 +49,7 @@ class ComponentTokenParser extends Twig_TokenParser throw new Twig_Error_Syntax( sprintf('Invalid syntax in the component tag. Line %s', $lineno), $stream->getCurrent()->getLine(), - $stream->getFilename() + $stream->getSourceContext() ); break; } diff --git a/modules/cms/twig/ContentTokenParser.php b/modules/cms/twig/ContentTokenParser.php index 9ca952123..2ce3cad35 100644 --- a/modules/cms/twig/ContentTokenParser.php +++ b/modules/cms/twig/ContentTokenParser.php @@ -53,7 +53,7 @@ class ContentTokenParser extends Twig_TokenParser throw new Twig_Error_Syntax( sprintf('Invalid syntax in the content tag. Line %s', $lineno), $stream->getCurrent()->getLine(), - $stream->getFilename() + $stream->getSourceContext() ); break; } diff --git a/modules/cms/twig/PartialTokenParser.php b/modules/cms/twig/PartialTokenParser.php index 3a9c068d2..57e452ec6 100644 --- a/modules/cms/twig/PartialTokenParser.php +++ b/modules/cms/twig/PartialTokenParser.php @@ -53,7 +53,7 @@ class PartialTokenParser extends Twig_TokenParser throw new Twig_Error_Syntax( sprintf('Invalid syntax in the partial tag. Line %s', $lineno), $stream->getCurrent()->getLine(), - $stream->getFilename() + $stream->getSourceContext() ); break; } diff --git a/modules/cms/twig/PlaceholderNode.php b/modules/cms/twig/PlaceholderNode.php index c1697a36c..62049d41d 100644 --- a/modules/cms/twig/PlaceholderNode.php +++ b/modules/cms/twig/PlaceholderNode.php @@ -14,9 +14,11 @@ class PlaceholderNode extends Twig_Node public function __construct($name, $paramValues, $body, $lineno, $tag = 'placeholder') { $nodes = []; + if ($body) { $nodes['default'] = $body; } + $attributes = $paramValues; $attributes['name'] = $name; diff --git a/modules/cms/twig/PlaceholderTokenParser.php b/modules/cms/twig/PlaceholderTokenParser.php index ac372f003..93e96c186 100644 --- a/modules/cms/twig/PlaceholderTokenParser.php +++ b/modules/cms/twig/PlaceholderTokenParser.php @@ -75,9 +75,9 @@ class PlaceholderTokenParser extends Twig_TokenParser default: throw new Twig_Error_Syntax( - sprintf('Invalid syntax in the placeholder tag. Line %s', $lineno), + sprintf('Invalid syntax in the placeholder tag. Line %s', $stream->getCurrent()->getLine()), $stream->getCurrent()->getLine(), - $stream->getFilename() + $stream->getSourceContext() ); break; } diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index b55bc52e0..bd368a917 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -9,7 +9,6 @@ use System\Helpers\View as ViewHelper; use System\Classes\PluginManager; use System\Classes\MarkupManager; use System\Twig\MailPartialTokenParser; -use System\Twig\MailComponentTokenParser; use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles; /** @@ -206,6 +205,7 @@ class MailManager public function renderPartial($code, array $params = []) { + traceLog($params); if (!$partial = MailPartial::findOrMakePartial($code)) { return ''; } @@ -257,8 +257,7 @@ class MailManager $markupManager = MarkupManager::instance(); $markupManager->beginTransaction(); $markupManager->registerTokenParsers([ - new MailPartialTokenParser, - new MailComponentTokenParser + new MailPartialTokenParser ]); } diff --git a/modules/system/models/mailbrandsetting/sample_template.htm b/modules/system/models/mailbrandsetting/sample_template.htm index 0214cd286..9d0d04fac 100644 --- a/modules/system/models/mailbrandsetting/sample_template.htm +++ b/modules/system/models/mailbrandsetting/sample_template.htm @@ -5,43 +5,43 @@ Cumque dicta doloremque eaque, enim error laboriosam pariatur possimus te ## Heading 2 -{% component 'table' %} +{% partial 'table' body %} | Item | Description | Price | |:------------- |:-------------:| --------:| | Item 1 | Centered | $10 | | Item 2 | Right-Aligned | $20 | -{% endcomponent %} +{% endpartial %} ### Heading 3 This is a paragraph filled with Lorem Ipsum and a link. Cumque dicta doloremque eaque, enim error laboriosam pariatur possimus tenetur veritatis voluptas. -{% component 'button' url='javascript:;' %} +{% partial 'button' url='javascript:;' body %} Primary button -{% endcomponent %} +{% endpartial %} -{% component 'button' type='positive' url='javascript:;' %} +{% partial 'button' type='positive' url='javascript:;' body %} Positive button -{% endcomponent %} +{% endpartial %} -{% component 'button' type='negative' url='javascript:;' %} +{% partial 'button' type='negative' url='javascript:;' body %} Negative button -{% endcomponent %} +{% endpartial %} -{% component 'panel' %} +{% partial 'panel' body %} How awesome is this panel? -{% endcomponent %} +{% endpartial %} Some more text -{% component 'promotion' %} +{% partial 'promotion' body %} Coupon code: OCTOBER -{% endcomponent %} +{% endpartial %} Thanks, {{ appName }} -{% component 'subcopy' %} +{% partial 'subcopy' body %} This is the subcopy of the email -{% endcomponent %} +{% endpartial %} diff --git a/modules/system/twig/MailComponentNode.php b/modules/system/twig/MailComponentNode.php deleted file mode 100644 index 82d735567..000000000 --- a/modules/system/twig/MailComponentNode.php +++ /dev/null @@ -1,51 +0,0 @@ - $nodes, 'body' => $body], ['names' => $paramNames], $lineno, $tag); - } - - /** - * Compiles the node to PHP. - * - * @param Twig_Compiler $compiler A Twig_Compiler instance - */ - public function compile(Twig_Compiler $compiler) - { - $compiler->addDebugInfo($this); - - $compiler->write("\$context['__system_component_params'] = [];\n"); - - $compiler - ->addDebugInfo($this) - ->write('ob_start();') - ->subcompile($this->getNode('body')) - ->write("\$context['__system_component_params']['body'] = ob_get_clean();"); - - for ($i = 1; $i < count($this->getNode('nodes')); $i++) { - $compiler->write("\$context['__system_component_params']['".$this->getAttribute('names')[$i-1]."'] = "); - $compiler->subcompile($this->getNode('nodes')->getNode($i)); - $compiler->write(";\n"); - } - - $compiler - ->write("echo \System\Classes\MailManager::instance()->renderPartial(") - ->subcompile($this->getNode('nodes')->getNode(0)) - ->write(", \$context['__system_component_params']") - ->write(");\n") - ; - - $compiler->write("unset(\$context['__system_component_params']);\n"); - } -} diff --git a/modules/system/twig/MailComponentTokenParser.php b/modules/system/twig/MailComponentTokenParser.php deleted file mode 100644 index 4edd98b07..000000000 --- a/modules/system/twig/MailComponentTokenParser.php +++ /dev/null @@ -1,82 +0,0 @@ -getLine(); - $stream = $this->parser->getStream(); - - $name = $this->parser->getExpressionParser()->parseExpression(); - $paramNames = []; - $nodes = [$name]; - - $end = false; - while (!$end) { - $current = $stream->next(); - - switch ($current->getType()) { - case Twig_Token::NAME_TYPE: - $paramNames[] = $current->getValue(); - $stream->expect(Twig_Token::OPERATOR_TYPE, '='); - $nodes[] = $this->parser->getExpressionParser()->parseExpression(); - break; - - case Twig_Token::BLOCK_END_TYPE: - $end = true; - break; - - default: - throw new Twig_Error_Syntax( - sprintf('Invalid syntax in the partial tag. Line %s', $lineno), - $stream->getCurrent()->getLine(), - $stream->getFilename() - ); - break; - } - } - - $body = $this->parser->subparse([$this, 'decideComponentEnd'], true); - $stream->expect(Twig_Token::BLOCK_END_TYPE); - - return new MailComponentNode(new Twig_Node($nodes), $paramNames, $body, $token->getLine(), $this->getTag()); - } - - public function decideComponentEnd(Twig_Token $token) - { - return $token->test('endcomponent'); - } - - /** - * Gets the tag name associated with this token parser. - * - * @return string The tag name - */ - public function getTag() - { - return 'component'; - } -} diff --git a/modules/system/twig/MailPartialNode.php b/modules/system/twig/MailPartialNode.php index 163329e70..9b54c536a 100644 --- a/modules/system/twig/MailPartialNode.php +++ b/modules/system/twig/MailPartialNode.php @@ -11,9 +11,15 @@ use Twig_Compiler; */ class MailPartialNode extends Twig_Node { - public function __construct(Twig_Node $nodes, $paramNames, $lineno, $tag = 'partial') + public function __construct(Twig_Node $nodes, $paramNames, $body, $lineno, $tag = 'partial') { - parent::__construct(['nodes' => $nodes], ['names' => $paramNames], $lineno, $tag); + $nodes = ['nodes' => $nodes]; + + if ($body) { + $nodes['body'] = $body; + } + + parent::__construct($nodes, ['names' => $paramNames], $lineno, $tag); } /** @@ -27,6 +33,14 @@ class MailPartialNode extends Twig_Node $compiler->write("\$context['__system_partial_params'] = [];\n"); + if ($this->hasNode('body')) { + $compiler + ->addDebugInfo($this) + ->write('ob_start();') + ->subcompile($this->getNode('body')) + ->write("\$context['__system_partial_params']['body'] = ob_get_clean();"); + } + for ($i = 1; $i < count($this->getNode('nodes')); $i++) { $compiler->write("\$context['__system_partial_params']['".$this->getAttribute('names')[$i-1]."'] = "); $compiler->subcompile($this->getNode('nodes')->getNode($i)); diff --git a/modules/system/twig/MailPartialTokenParser.php b/modules/system/twig/MailPartialTokenParser.php index 2b68c5a10..d0f273ccf 100644 --- a/modules/system/twig/MailPartialTokenParser.php +++ b/modules/system/twig/MailPartialTokenParser.php @@ -33,11 +33,21 @@ class MailPartialTokenParser extends Twig_TokenParser $name = $this->parser->getExpressionParser()->parseExpression(); $paramNames = []; $nodes = [$name]; + $hasBody = false; + $body = null; $end = false; while (!$end) { $current = $stream->next(); + if ( + $current->test(Twig_Token::NAME_TYPE, 'body') && + !$stream->test(Twig_Token::OPERATOR_TYPE, '=') + ) { + $hasBody = true; + $current = $stream->next(); + } + switch ($current->getType()) { case Twig_Token::NAME_TYPE: $paramNames[] = $current->getValue(); @@ -53,13 +63,23 @@ class MailPartialTokenParser extends Twig_TokenParser throw new Twig_Error_Syntax( sprintf('Invalid syntax in the partial tag. Line %s', $lineno), $stream->getCurrent()->getLine(), - $stream->getFilename() + $stream->getSourceContext() ); break; } } - return new MailPartialNode(new Twig_Node($nodes), $paramNames, $token->getLine(), $this->getTag()); + if ($hasBody) { + $body = $this->parser->subparse([$this, 'decidePartialEnd'], true); + $stream->expect(Twig_Token::BLOCK_END_TYPE); + } + + return new MailPartialNode(new Twig_Node($nodes), $paramNames, $body, $token->getLine(), $this->getTag()); + } + + public function decidePartialEnd(Twig_Token $token) + { + return $token->test('endpartial'); } /** diff --git a/modules/system/views/mail/layout-default.htm b/modules/system/views/mail/layout-default.htm index 95401bbc0..2cb895691 100644 --- a/modules/system/views/mail/layout-default.htm +++ b/modules/system/views/mail/layout-default.htm @@ -17,9 +17,9 @@ name = "Default layout"
| @@ -42,9 +42,9 @@ name = "Default layout" |