Skip to content

Commit

Permalink
Change to mail partial syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Jul 26, 2017
1 parent f1ac7ee commit 18b9253
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 171 deletions.
8 changes: 4 additions & 4 deletions modules/backend/views/mail/invite.htm
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

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.
4 changes: 2 additions & 2 deletions modules/backend/views/mail/restore.htm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

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 %}
2 changes: 1 addition & 1 deletion modules/cms/twig/ComponentTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function parse(Twig_Token $token)
throw new Twig_Error_Syntax(
sprintf('Invalid syntax in the component tag. Line %s', $lineno),
$stream->getCurrent()->getLine(),
$stream->getFilename()
$stream->getSourceContext()
);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/cms/twig/ContentTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function parse(Twig_Token $token)
throw new Twig_Error_Syntax(
sprintf('Invalid syntax in the content tag. Line %s', $lineno),
$stream->getCurrent()->getLine(),
$stream->getFilename()
$stream->getSourceContext()
);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion modules/cms/twig/PartialTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ 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;
}
Expand Down
2 changes: 2 additions & 0 deletions modules/cms/twig/PlaceholderNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions modules/cms/twig/PlaceholderTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ protected function loadParams($stream)

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;
}
Expand Down
5 changes: 2 additions & 3 deletions modules/system/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 modules/system/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 modules/system/twig/MailComponentNode.php

This file was deleted.

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

This file was deleted.

18 changes: 16 additions & 2 deletions modules/system/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 modules/system/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
Loading

0 comments on commit 18b9253

Please sign in to comment.