Skip to content

Commit

Permalink
allows {else} in {first}, {last}, {sep} [Closes #247]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 4, 2021
1 parent af5478a commit f3d6823
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Latte/Macros/CoreMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function macroElse(MacroNode $node, PhpWriter $writer): string
if ($node->args !== '' && Helpers::startsWith($node->args, 'if')) {
throw new CompileException('Arguments are not allowed in {else}, did you mean {elseif}?');
}
$node->validate(false, ['if', 'ifset', 'foreach', 'ifchanged', 'try']);
$node->validate(false, ['if', 'ifset', 'foreach', 'ifchanged', 'try', 'first', 'last', 'sep']);

$parent = $node->parentNode;
if (isset($parent->data->else)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/Latte/CoreMacros.first-sep-last.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ $template = <<<'EOD'
{/foreach}
{foreach $people as $person}
{first}({else}[{/first} {$person}{sep}, {else};{/sep} {last}){else}]{/last}
{/foreach}
{foreach $people as $person}
{first 2}({/first} {$person}{sep 2}, {/sep} {last 2}){/last}
{/foreach}
Expand Down
5 changes: 5 additions & 0 deletions tests/Latte/expected/CoreMacros.first-sep-last.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
Paul )


( John, ]
[ Mary, ]
[ Paul; )


( John,
Mary )
( Paul )
Expand Down
37 changes: 34 additions & 3 deletions tests/Latte/expected/CoreMacros.first-sep-last.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,42 @@ final class Template%a% extends Latte\Runtime\Template
$iterations = 0;
foreach ($iterator = $ʟ_it = new LR\CachingIterator($people, $ʟ_it ?? null) as $person) {
echo ' ';
if ($iterator->isFirst(2)) {
if ($iterator->isFirst()) {
echo '(';
}
else {
echo '[';
}
echo ' ';
echo LR\Filters::escapeHtmlText($person) /* line 8 */;
if (!$iterator->isLast()) {
echo ', ';
}
else {
echo ';';
}
echo ' ';
if ($iterator->isLast()) {
echo ')';
}
else {
echo ']';
}
echo "\n";
$iterations++;
}
$iterator = $ʟ_it = $ʟ_it->getParent();
echo '
';
$iterations = 0;
foreach ($iterator = $ʟ_it = new LR\CachingIterator($people, $ʟ_it ?? null) as $person) {
echo ' ';
if ($iterator->isFirst(2)) {
echo '(';
}
echo ' ';
echo LR\Filters::escapeHtmlText($person) /* line 13 */;
if (!$iterator->isLast(2)) {
echo ', ';
}
Expand All @@ -59,7 +90,7 @@ final class Template%a% extends Latte\Runtime\Template
echo '(';
}
echo ' ';
echo LR\Filters::escapeHtmlText($person) /* line 13 */;
echo LR\Filters::escapeHtmlText($person) /* line 18 */;
if (!$iterator->isLast(1)) {
echo ', ';
}
Expand All @@ -80,7 +111,7 @@ final class Template%a% extends Latte\Runtime\Template
echo ' <span>(</span>';
}
echo ' ';
echo LR\Filters::escapeHtmlText($person) /* line 18 */;
echo LR\Filters::escapeHtmlText($person) /* line 23 */;
if (!$iterator->isLast()) {
echo '<span>, </span>';
}
Expand Down

0 comments on commit f3d6823

Please sign in to comment.