diff --git a/src/Latte/Macros/CoreMacros.php b/src/Latte/Macros/CoreMacros.php
index e55a01e648..d8db5d1b10 100644
--- a/src/Latte/Macros/CoreMacros.php
+++ b/src/Latte/Macros/CoreMacros.php
@@ -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)) {
diff --git a/tests/Latte/CoreMacros.first-sep-last.phpt b/tests/Latte/CoreMacros.first-sep-last.phpt
index cf35585dc6..65fb718ba4 100644
--- a/tests/Latte/CoreMacros.first-sep-last.phpt
+++ b/tests/Latte/CoreMacros.first-sep-last.phpt
@@ -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}
diff --git a/tests/Latte/expected/CoreMacros.first-sep-last.html b/tests/Latte/expected/CoreMacros.first-sep-last.html
index a229315173..fb45772cb0 100644
--- a/tests/Latte/expected/CoreMacros.first-sep-last.html
+++ b/tests/Latte/expected/CoreMacros.first-sep-last.html
@@ -4,6 +4,11 @@
Paul )
+ ( John, ]
+ [ Mary, ]
+ [ Paul; )
+
+
( John,
Mary )
( Paul )
diff --git a/tests/Latte/expected/CoreMacros.first-sep-last.phtml b/tests/Latte/expected/CoreMacros.first-sep-last.phtml
index e69b8835d9..5577a0d6d5 100644
--- a/tests/Latte/expected/CoreMacros.first-sep-last.phtml
+++ b/tests/Latte/expected/CoreMacros.first-sep-last.phtml
@@ -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 ', ';
}
@@ -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 ', ';
}
@@ -80,7 +111,7 @@ final class Template%a% extends Latte\Runtime\Template
echo ' (';
}
echo ' ';
- echo LR\Filters::escapeHtmlText($person) /* line 18 */;
+ echo LR\Filters::escapeHtmlText($person) /* line 23 */;
if (!$iterator->isLast()) {
echo ', ';
}