Skip to content

Commit cca677f

Browse files
committed
Fix multi-segment if/unless condition compilation
Resolves #15
1 parent 59f5bc2 commit cca677f

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/Compiler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,10 @@ private function canInlineConditional(BlockStatement $block, string $helperName,
326326

327327
/**
328328
* Compile the condition expression for an inlined if/unless ternary.
329-
* For simple single-segment paths, routes through cv() which already resolves closures,
330-
* so ifvar() suffices. For all other expressions, closures at nested paths are not
331-
* invoked (not a real-world or spec concern).
329+
* Single-segment plain context paths (e.g. {{#if foo}}) use cv() so that closures are
330+
* invoked before being tested. All other expressions (multi-segment paths, data variables,
331+
* block params, sub-expressions) use compileExpression() as a helper argument.
332+
* Closures at nested path segments are not invoked.
332333
* @param bool $negate true for `unless` or inverted `{{^if}}`
333334
*/
334335
private function compileConditionalExpr(Expression $condExpr, bool $negate): string
@@ -338,6 +339,7 @@ private function compileConditionalExpr(Expression $condExpr, bool $negate): str
338339
&& !$condExpr->data
339340
&& $condExpr->depth === 0
340341
&& is_string($part)
342+
&& count($condExpr->parts) === 1
341343
&& !self::scopedId($condExpr)
342344
&& $this->lookupBlockParam($part) === null
343345
) {

tests/RegressionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,21 @@ public static function ifElseProvider(): array
18451845
_tpl,
18461846
'expected' => " Finally, do this last thing if all else fails\n",
18471847
],
1848+
1849+
[
1850+
'desc' => '#15 - if should work with multi-segment path expression',
1851+
'template' => '{{#if foo.bar}}bad{{else}}OK{{/if}}',
1852+
'data' => ['foo' => 'foo'],
1853+
'expected' => 'OK',
1854+
],
1855+
1856+
[
1857+
'desc' => 'strict mode should not throw if the final property of a helper argument is missing',
1858+
'template' => '{{#if foo.bar}}bad{{else}}OK{{/if}}',
1859+
'options' => new Options(strict: true),
1860+
'data' => ['foo' => []],
1861+
'expected' => 'OK',
1862+
],
18481863
];
18491864
}
18501865

0 commit comments

Comments
 (0)