Skip to content

Commit 2fa2dc5

Browse files
committed
Fix inline partials leaking out of an else block
1 parent 125fe8b commit 2fa2dc5

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

src/HelperOptions.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,7 @@ public function fn(mixed $context = Scope::Use, mixed $data = null): string
7171
}
7272
return '';
7373
}
74-
$cx = $this->cx;
75-
// Save inlinePartials so that any {{#* inline}} partials registered inside the block body
76-
// don't leak out after fn() returns. The spec requires inline partials to be
77-
// block-scoped. PHP copy-on-write makes this assignment cheap when no inline partials are registered.
78-
$savedInlinePartials = $cx->inlinePartials;
79-
$ret = $this->invokeBlock($this->cb, $context, $data);
80-
$cx->inlinePartials = $savedInlinePartials;
81-
return $ret;
74+
return $this->invokeBlock($this->cb, $context, $data);
8275
}
8376

8477
public function inverse(mixed $context = Scope::Use, mixed $data = null): string
@@ -95,6 +88,10 @@ public function inverse(mixed $context = Scope::Use, mixed $data = null): string
9588
private function invokeBlock(\Closure $closure, mixed $context, mixed $data): string
9689
{
9790
$cx = $this->cx;
91+
// Save inlinePartials so that any {{#* inline}} partials registered inside the block body
92+
// don't leak out after it returns. The spec requires inline partials to be
93+
// block-scoped. PHP copy-on-write makes this assignment cheap when no inline partials are registered.
94+
$savedInlinePartials = $cx->inlinePartials;
9895
$scope = $this->scope;
9996
// Skip depths push when the caller explicitly passes the current scope (equivalent to
10097
// HBS.js options.fn(this) / options.inverse(this)), since the scope level isn't changing.
@@ -129,6 +126,7 @@ private function invokeBlock(\Closure $closure, mixed $context, mixed $data): st
129126
if ($savedFrame !== null) {
130127
$cx->frame = $savedFrame;
131128
}
129+
$cx->inlinePartials = $savedInlinePartials;
132130
return $ret;
133131
}
134132

tests/RegressionTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,10 @@ public static function sectionProvider(): array
19781978
'data' => ['section' => ['x' => 1]],
19791979
'expected' => 'INSIDEBEFORE',
19801980
],
1981+
'inline partials registered inside an else block do not leak out after the section ends' => [
1982+
'template' => '{{#* inline "p"}}BEFORE{{/inline}}{{#foo}}{{else}}{{#* inline "p"}}INSIDE{{/inline}}{{> p}}{{/foo}}{{> p}}',
1983+
'expected' => 'INSIDEBEFORE',
1984+
],
19811985
];
19821986
}
19831987

0 commit comments

Comments
 (0)