Skip to content

Commit cf3b6cb

Browse files
committed
Correctly invoke @data vars passed to if/unless
Also added regression test for `@data` variables in partial block context.
1 parent f1db270 commit cf3b6cb

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

src/Compiler.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,13 @@ private function canInlineConditional(BlockStatement $block, string $helperName,
346346
private function compileConditionalExpr(Expression $condExpr, bool $negate): string
347347
{
348348
$part = $condExpr instanceof PathExpression ? ($condExpr->parts[0] ?? null) : null;
349-
if ($condExpr instanceof PathExpression
350-
&& !$condExpr->data
349+
$isSimplePath = $condExpr instanceof PathExpression
351350
&& $condExpr->depth === 0
352351
&& is_string($part)
353-
&& count($condExpr->parts) === 1
354-
&& !self::scopedId($condExpr)
355-
&& $this->lookupBlockParam($part) === null
356-
) {
352+
&& count($condExpr->parts) === 1;
353+
if ($isSimplePath && $condExpr->data) {
354+
$val = self::getRuntimeFunc('dv', $this->compileExpression($condExpr));
355+
} elseif ($isSimplePath && !self::scopedId($condExpr) && $this->lookupBlockParam($part) === null) {
357356
$val = self::getRuntimeFunc('cv', '$in, ' . self::quote($part));
358357
} else {
359358
$savedHelperArgs = $this->compilingHelperArgs;

tests/RegressionTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,12 @@ public static function ifElseProvider(): array
18461846
'data' => ['items' => [['name' => 'Item', 'falsy' => false]], 'name' => 'Root'],
18471847
'expected' => 'Root',
18481848
],
1849+
1850+
'closure in @data variable passed to if is invoked' => [
1851+
'template' => '{{#if @foo}}bad{{else}}Executed with no args{{/if}}',
1852+
'vars' => ['foo' => fn(...$args) => count($args)],
1853+
'expected' => 'Executed with no args',
1854+
],
18491855
];
18501856
}
18511857

0 commit comments

Comments
 (0)