Skip to content

Add opt-in block-scoped inline partials (Compatibility.BlockScopedInlinePartials) - #688

Merged
rexm merged 5 commits into
masterfrom
robot/issue-673
Nov 22, 2026
Merged

rexm merged 5 commits into
masterfrom
robot/issue-673

Conversation

@stencil-harness

Copy link
Copy Markdown
Contributor

Refs #673

Summary

An inline partial declared inside a block ({{#with}}, {{#each}}, {{#> layout}}, a custom block helper) or inside an included partial is written to the enclosing scope and never removed. It can still be called after the block ends, and it permanently replaces an outer partial with the same name. In handlebars.js such a partial is visible only in its block and that block's children.

This PR adds handlebars.js-style scoping as an opt-in setting:

handlebars.Configuration.Compatibility.BlockScopedInlinePartials = true;

The default is false, and existing behavior is unchanged. The flag is read at compile time; when it is off, the decorator code path is identical to master.

Why opt-in rather than a new default

Real templates depend on the current behavior. On #673 I ran these patterns against both versions:

Template Today / default With the flag on
{{> macros}}{{> button label="OK"}} where partial macros declares {{#*inline "button"}} <button>OK</button> throws "could not be resolved"
{{#with customer}}{{#*inline "addr"}}…{{/inline}}{{/with}} … {{> addr}} renders throws
{{#> layout}}{{#*inline "content"}}Hi{{/inline}}{{/layout}}<aside>{{> content}}</aside> <main>Hi</main><aside>Hi</aside> throws
{{#*inline "p"}}outer{{/inline}}{{#with .}}{{#*inline "p"}}inner{{/inline}}{{> p}}{{/with}} {{> p}} inner inner inner outer

Turning this on by default would break the "macro library" pattern (first row) in particular. Whether to change the default in a future major release is left to the maintainers.

Changes

  • Compatibility.BlockScopedInlinePartials (new, default false), documented in the README alongside the other compatibility flags.
  • DecoratorDefinitionsExtensions.Compile: when the flag is on, each block's whole decorator chain runs inside a scope on the frame's inline partials. The previous set is saved before the decorators run and restored (try/finally) after the decorated block renders. Wrapping the whole chain, rather than only the *inline decorator, also handles a custom decorator placed between two {{#*inline}} declarations.
  • CascadeIndex: internal BeginScope() / EndScope() (copy-on-scope of the frame's own entries).

Known limitation (unchanged by this PR)

{{#if}} / {{#unless}} bodies are compiled inline into the enclosing block. An {{#*inline}} inside them is therefore registered on the enclosing scope, and it is registered even when the condition is false. This happens in both modes and is a separate existing issue.

Tests

  • InlinePartialTests, flag on: scoped to the declaring block; outer definition restored; visible to nested blocks; #each; last definition in a block wins; custom decorator between inlines; {{#> layout}} partial-block scoping; repeated renders.
  • InlinePartialTests, default: block-declared partials stay registered after the block, and partials declared in an included partial remain available (legacy behavior pinned).
  • Full suite passes (2210 of 2210). The library builds for netstandard2.0, netstandard2.1, net8.0 and net10.0.

Release

This is additive and doesn't change default behavior, so it fits a minor release. Suggested release-notes entry: "New: Compatibility.BlockScopedInlinePartials opt-in for handlebars.js inline-partial scoping (#673)". No action is needed from users who don't set the flag. To roll back after enabling it, set the flag back to false.

🤖 Generated with Claude Code

Closes #673

rexm and others added 3 commits November 7, 2026 20:09
Inline partials declared with {{#*inline}} inside a block were written
into the enclosing frame and never removed, so they stayed resolvable
after the block closed and permanently replaced outer definitions with
the same name. Open a partials scope around each block's decorator chain
and restore the previous set once the decorated block has rendered,
matching handlebars.js.

Fixes #673

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…kScopedInlinePartials

Scoping inline partials to their declaring block (handlebars.js
semantics) breaks templates that call a block's or an included
partial's inline partials after it ends. Keep the existing behavior by
default and enable scoping only when
Compatibility.BlockScopedInlinePartials is true. The flag is read at
compile time, so the default path is unchanged.

Refs #673

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sonarqubecloud

Copy link
Copy Markdown

@rexm
rexm merged commit 74bf99f into master Nov 22, 2026
7 checks passed
@rexm
rexm deleted the robot/issue-673 branch November 22, 2026 05:55
stencil-harness Bot added a commit that referenced this pull request Nov 22, 2026
…to true

#688 merged and shipped in 2.4.4 with Compatibility.BlockScopedInlinePartials
as an opt-in. Instead of replacing it with a new LegacyInlinePartialScoping
flag (which would remove a released public property), keep the released
property and flip its default to true. Setting it to false is the opt-out,
and the unresolved-partial exception names that.

Refs #690

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@stencil-harness stencil-harness Bot mentioned this pull request Nov 22, 2026
jsneedles pushed a commit to jsneedles/Handlebars.Net that referenced this pull request Nov 25, 2026
…inePartialScoping opt-out

Brings in the block-scoped inline partial implementation from Handlebars-Net#688 (which
added it as the opt-in Compatibility.BlockScopedInlinePartials) and inverts
it: handlebars.js scoping is now the default, and the earlier behavior is
restored with Compatibility.LegacyInlinePartialScoping = true.

The "Referenced partial name '...' could not be resolved" exception now
names the opt-out when it is not set.

Refs Handlebars-Net#673, Handlebars-Net#690

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inline partials declared inside a block stay registered after the block ends

1 participant