Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"spatie/invade": "^1.1"
},
"autoload": {
"psr-4": {
Expand Down
68 changes: 67 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 55 additions & 2 deletions src/Actions/Templates/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MailCarrier\Actions\Templates;

use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
use Latte;
use MailCarrier\Actions\Action;
use MailCarrier\Models\Template;
Expand All @@ -23,10 +25,61 @@ public function run(Template $template, array $variables = []): string

$latte = new Latte\Engine();
$latte->setLoader(new Latte\Loaders\StringLoader([
$mainFileName => $mainFileContent,
$layoutFileName => $template->layout?->content,
$mainFileName => $this->parse($mainFileContent),
$layoutFileName => $this->parse($template->layout?->content ?: ''),
]));

return $latte->renderToString($mainFileName, $variables);
}

/**
* Parse the template syntax.
*/
protected function parse(string $template): string
{
preg_match_all('/{{(.*?)}}/', $template, $variables);

foreach ($variables[1] ?? [] as $variable) {
$realVariable = Str::of($variable)
->before('|')
->trim()
->toString();

// Extract filters to reapply them when replacing the variable
$variableFilters = '';

if (str_contains($variable, '|')) {
$variableFilters = Str::of($variable)
->after('|')
->trim()
->whenNotEmpty(fn (Stringable $str) => $str->prepend('|'))
->toString();
}

// Transform variable if is an array/object
if (str_contains($realVariable, '.')) {
// Transform everything after the actual variable name into an array-based index
$indexBasedVariable = Str::of($realVariable)
->after('.')
->explode('.')
->map(fn (string $value) => is_numeric($value) ? "[$value]" : "['$value']")
->join('');

// Prepend the actual variable name
$realVariable = Str::before($realVariable, '.') . $indexBasedVariable;
}

if (!str_starts_with($realVariable, '$')) {
$realVariable = '$' . $realVariable;
}

$template = str_replace(
'{{' . $variable . '}}',
'{' . $realVariable . $variableFilters . '}',
$template
);
}

return $template;
}
}
53 changes: 53 additions & 0 deletions tests/Unit/Template/RenderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

use MailCarrier\Actions\Templates\Render;

it('transforms plain variables', function () {
$result = invade(Render::resolve())->parse(<<<'TEMPLATE'
{{foo}} {{$bar}} {{foo}} {{bar}}
TEMPLATE);

expect($result)->toBe(<<<'TEMPLATE'
{$foo} {$bar} {$foo} {$bar}
TEMPLATE);
});

it('transforms direct access to array properties', function () {
$result = invade(Render::resolve())->parse(<<<'TEMPLATE'
{{foo['bar']}} {{$bar['baz']}} {{foo['bar']['baz']}} {{bar['baz']}} {{bar[0]}} {{bar[0][1]}}
TEMPLATE);

expect($result)->toBe(<<<'TEMPLATE'
{$foo['bar']} {$bar['baz']} {$foo['bar']['baz']} {$bar['baz']} {$bar[0]} {$bar[0][1]}
TEMPLATE);
});

it('transforms dot notation access to array properties', function () {
$result = invade(Render::resolve())->parse(<<<'TEMPLATE'
{{foo.bar}} {{$bar.baz}} {{foo['bar']['baz']}} {{bar.baz}} {{foo[0].bar}} {{foo.0.bar.baz}} {{foo.0.1.bar.2.baz}}
TEMPLATE);

expect($result)->toBe(<<<'TEMPLATE'
{$foo['bar']} {$bar['baz']} {$foo['bar']['baz']} {$bar['baz']} {$foo[0]['bar']} {$foo[0]['bar']['baz']} {$foo[0][1]['bar'][2]['baz']}
TEMPLATE);
});

it('handles variables with spaces around brackets', function () {
$result = invade(Render::resolve())->parse(<<<'TEMPLATE'
{{ foo.bar }} {{ $bar.baz }} {{ foo['bar']['baz'] }} {{foo.bar}} {{ foo }}
TEMPLATE);

expect($result)->toBe(<<<'TEMPLATE'
{$foo['bar']} {$bar['baz']} {$foo['bar']['baz']} {$foo['bar']} {$foo}
TEMPLATE);
});

it('respects the engine filters', function () {
$result = invade(Render::resolve())->parse(<<<'TEMPLATE'
{{ foo.bar|upper }} {{$bar.baz|lower}} {{foo['bar']['baz']|upper}} {{ bar|upper|reverse }} {{foo|lower|reverse}}
TEMPLATE);

expect($result)->toBe(<<<'TEMPLATE'
{$foo['bar']|upper} {$bar['baz']|lower} {$foo['bar']['baz']|upper} {$bar|upper|reverse} {$foo|lower|reverse}
TEMPLATE);
});