Skip to content

Commit

Permalink
Change to mail partial syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Georges committed Jul 26, 2017
1 parent 281cfd3 commit 6c29bea
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 160 deletions.
5 changes: 2 additions & 3 deletions classes/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use System\Classes\PluginManager;
use System\Classes\MarkupManager;
use System\Twig\MailPartialTokenParser;
use System\Twig\MailComponentTokenParser;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;

/**
Expand Down Expand Up @@ -206,6 +205,7 @@ public function renderTextTemplate($template, $data = [])

public function renderPartial($code, array $params = [])
{
traceLog($params);
if (!$partial = MailPartial::findOrMakePartial($code)) {
return '<!-- Missing partial: '.$code.' -->';
}
Expand Down Expand Up @@ -257,8 +257,7 @@ protected function startTwig()
$markupManager = MarkupManager::instance();
$markupManager->beginTransaction();
$markupManager->registerTokenParsers([
new MailPartialTokenParser,
new MailComponentTokenParser
new MailPartialTokenParser
]);
}

Expand Down
28 changes: 14 additions & 14 deletions models/mailbrandsetting/sample_template.htm
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,43 @@

## 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 <a>doloremque eaque</a>, 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 %}
51 changes: 0 additions & 51 deletions twig/MailComponentNode.php

This file was deleted.

82 changes: 0 additions & 82 deletions twig/MailComponentTokenParser.php

This file was deleted.

18 changes: 16 additions & 2 deletions twig/MailPartialNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@
*/
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);
}

/**
Expand All @@ -27,6 +33,14 @@ public function compile(Twig_Compiler $compiler)

$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));
Expand Down
24 changes: 22 additions & 2 deletions twig/MailPartialTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,21 @@ public function parse(Twig_Token $token)
$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();
Expand All @@ -53,13 +63,23 @@ public function parse(Twig_Token $token)
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');
}

/**
Expand Down
8 changes: 4 additions & 4 deletions views/mail/layout-default.htm
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<table class="wrapper layout-default" width="100%" cellpadding="0" cellspacing="0">

<!-- Header -->
{% component 'header' %}
{% partial 'header' body %}
{{ subject }}
{% endcomponent %}
{% endpartial %}

<tr>
<td align="center">
Expand All @@ -42,9 +42,9 @@
</tr>

<!-- Footer -->
{% component 'footer' %}
{% partial 'footer' body %}
&copy; {{ "now"|date("Y") }} {{ appName }}. All rights reserved.
{% endcomponent %}
{% endpartial %}

</table>

Expand Down
4 changes: 2 additions & 2 deletions views/mail/layout-system.htm
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
{{ content|raw }}

<!-- Subcopy -->
{% component 'subcopy' %}
{% partial 'subcopy' body %}
**This is an automatic message. Please do not reply to it.**
{% endcomponent %}
{% endpartial %}
</td>
</tr>
</table>
Expand Down

0 comments on commit 6c29bea

Please sign in to comment.