From 6334d98e58fc7743b22d34462a35f23cb4ae7751 Mon Sep 17 00:00:00 2001 From: Rex Morgan Date: Sat, 7 Nov 2026 20:09:20 -0500 Subject: [PATCH 1/2] fix: scope inline partials to the block that declares them 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 --- source/Handlebars.Test/InlinePartialTests.cs | 105 ++++++++++++++++++ source/Handlebars/Collections/CascadeIndex.cs | 16 +++ .../Expression/DecoratorDefinition.cs | 33 +++++- 3 files changed, 153 insertions(+), 1 deletion(-) diff --git a/source/Handlebars.Test/InlinePartialTests.cs b/source/Handlebars.Test/InlinePartialTests.cs index 51c415a6..5da7b22c 100644 --- a/source/Handlebars.Test/InlinePartialTests.cs +++ b/source/Handlebars.Test/InlinePartialTests.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using HandlebarsDotNet.Compiler; using Xunit; namespace HandlebarsDotNet.Test @@ -484,6 +485,110 @@ public void BlockInlinePartialWithInlinePartials() var result4 = template(data); Assert.Equal("Hello Pete", result4); } + + // https://github.com/Handlebars-Net/Handlebars.Net/issues/673 + // handlebars.js spec/partials.js: "should define inline partials for block" + [Fact] + public void InlinePartialIsScopedToDeclaringBlock() + { + var handlebars = Handlebars.Create(); + + var inside = handlebars.Compile("{{#with .}}{{#*inline \"myPartial\"}}success{{/inline}}{{> myPartial}}{{/with}}"); + Assert.Equal("success", inside(new { })); + + var outside = handlebars.Compile("{{#with .}}{{#*inline \"myPartial\"}}success{{/inline}}{{/with}}{{> myPartial}}"); + var exception = Assert.Throws(() => outside(new { })); + Assert.Contains("'myPartial' could not be resolved", exception.Message); + } + + [Fact] + public void InlinePartialInBlockOverridesOuterOnlyInsideBlock() + { + var handlebars = Handlebars.Create(); + var template = handlebars.Compile( + "{{#*inline \"myPartial\"}}outer{{/inline}}" + + "{{#with .}}{{#*inline \"myPartial\"}}inner{{/inline}}{{> myPartial}}{{/with}} " + + "{{> myPartial}}"); + + Assert.Equal("inner outer", template(new { })); + } + + [Fact] + public void InlinePartialInBlockIsVisibleToNestedBlocks() + { + var handlebars = Handlebars.Create(); + var template = handlebars.Compile( + "{{#with .}}{{#*inline \"myPartial\"}}success{{/inline}}" + + "{{#with .}}{{#if true}}{{> myPartial}}{{/if}}{{/with}}{{/with}}"); + + Assert.Equal("success", template(new { })); + } + + [Fact] + public void InlinePartialInEachIsScopedToLoop() + { + var handlebars = Handlebars.Create(); + var template = handlebars.Compile( + "{{#*inline \"item\"}}outer{{/inline}}" + + "{{#each items}}{{#*inline \"item\"}}[{{.}}]{{/inline}}{{> item}}{{/each}} {{> item}}"); + + Assert.Equal("[1][2] outer", template(new { items = new[] { 1, 2 } })); + } + + [Fact] + public void LastInlinePartialDefinitionInBlockWins() + { + var handlebars = Handlebars.Create(); + var template = handlebars.Compile( + "{{#with .}}{{#*inline \"p\"}}first{{/inline}}{{#*inline \"p\"}}second{{/inline}}{{> p}}{{/with}}"); + + Assert.Equal("second", template(new { })); + } + + [Fact] + public void InlinePartialsInBlockWithCustomDecoratorAreScopedToBlock() + { + var handlebars = Handlebars.Create(); + handlebars.RegisterDecorator("noop", + (TemplateDelegate function, in DecoratorOptions options, in Context context, in Arguments arguments) => function); + + var template = handlebars.Compile( + "{{#with .}}{{#*inline \"a\"}}A{{/inline}}{{*noop}}{{#*inline \"b\"}}B{{/inline}}{{> a}}{{> b}}{{/with}}{{> a}}"); + + var exception = Assert.Throws(() => template(new { })); + Assert.Contains("'a' could not be resolved", exception.Message); + + var inside = handlebars.Compile( + "{{#with .}}{{#*inline \"a\"}}A{{/inline}}{{*noop}}{{#*inline \"b\"}}B{{/inline}}{{> a}}{{> b}}{{/with}}"); + Assert.Equal("AB", inside(new { })); + } + + [Fact] + public void InlinePartialInPartialBlockIsScopedToPartialBlock() + { + var handlebars = Handlebars.Create(); + handlebars.RegisterTemplate("layout", "<{{> content}}>"); + + var template = handlebars.Compile( + "{{#> layout}}{{#*inline \"content\"}}inside{{/inline}}{{/layout}}"); + Assert.Equal("", template(new { })); + + var after = handlebars.Compile( + "{{#> layout}}{{#*inline \"content\"}}inside{{/inline}}{{/layout}}{{> content}}"); + var exception = Assert.Throws(() => after(new { })); + Assert.Contains("'content' could not be resolved", exception.Message); + } + + [Fact] + public void InlinePartialScopeIsRestoredOnRepeatedRenders() + { + var handlebars = Handlebars.Create(); + var template = handlebars.Compile( + "{{#*inline \"p\"}}outer{{/inline}}{{#with .}}{{#*inline \"p\"}}inner{{/inline}}{{> p}}{{/with}}{{> p}}"); + + Assert.Equal("innerouter", template(new { })); + Assert.Equal("innerouter", template(new { })); + } } } diff --git a/source/Handlebars/Collections/CascadeIndex.cs b/source/Handlebars/Collections/CascadeIndex.cs index 76227c6f..f0844756 100644 --- a/source/Handlebars/Collections/CascadeIndex.cs +++ b/source/Handlebars/Collections/CascadeIndex.cs @@ -48,6 +48,22 @@ public void Clear() _inner?.Clear(); } + /// + /// Makes subsequent calls write to a copy of the current own entries. + /// Pass the returned snapshot to to discard everything added since. + /// + internal DictionarySlim? BeginScope() + { + var snapshot = _inner; + if (snapshot != null) _inner = new DictionarySlim(snapshot); + return snapshot; + } + + internal void EndScope(DictionarySlim? snapshot) + { + _inner = snapshot; + } + public bool ContainsKey(in TKey key) { return (_inner?.ContainsKey(key) ?? false) diff --git a/source/Handlebars/Compiler/Translation/Expression/DecoratorDefinition.cs b/source/Handlebars/Compiler/Translation/Expression/DecoratorDefinition.cs index 50343553..d8e9c32e 100644 --- a/source/Handlebars/Compiler/Translation/Expression/DecoratorDefinition.cs +++ b/source/Handlebars/Compiler/Translation/Expression/DecoratorDefinition.cs @@ -51,7 +51,38 @@ CompilationContext context f(writer, bindingContext, current(writer, bindingContext, function)); } - return decorator; + // Inline partials ({{#*inline}}) declared by a block's decorators are only visible + // while that block renders (handlebars.js semantics): open a partials scope on the + // frame before the decorators run and restore the previous set once the decorated + // function has executed. + var chain = decorator; + return (EncodedTextWriter writer, BindingContext bindingContext, TemplateDelegate function) => + { + var partials = bindingContext.InlinePartialTemplates; + var snapshot = partials.BeginScope(); + TemplateDelegate decorated; + try + { + decorated = chain(writer, bindingContext, function); + } + catch + { + partials.EndScope(snapshot); + throw; + } + + return (EncodedTextWriter w, BindingContext c) => + { + try + { + decorated(w, c); + } + finally + { + partials.EndScope(snapshot); + } + }; + }; } } } \ No newline at end of file From ca5b8f133c65411a9113a041edf1dc8b9b2f13f3 Mon Sep 17 00:00:00 2001 From: "stencil-harness[bot]" <353851134+stencil-harness[bot]@users.noreply.github.com> Date: Sat, 21 Nov 2026 09:46:12 -0500 Subject: [PATCH 2/2] feat: make block-scoped inline partials opt-in via Compatibility.BlockScopedInlinePartials 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 --- README.md | 24 +++++++++ source/Handlebars.Test/InlinePartialTests.cs | 51 +++++++++++++++---- .../Expression/DecoratorDefinition.cs | 2 + .../Handlebars/Configuration/Compatibility.cs | 11 ++++ 4 files changed, 79 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index bbf4bb99..371eb9f3 100644 --- a/README.md +++ b/README.md @@ -467,6 +467,30 @@ public void LegacyStandaloneCaret() } ``` +#### `BlockScopedInlinePartials` +If `true`, an inline partial (`{{#*inline}}`) declared inside a block (`{{#with}}`, `{{#each}}`, `{{#> layout}}`, a custom block helper) or inside an included partial is visible only in that block and its children, as in Handlebarsjs. After the block it can no longer be called, and an outer partial with the same name is visible again. +By default (`false`) the earlier Handlebars.Net behavior is kept: such partials stay registered for the rest of the enclosing template. + +Before enabling, move any inline partial you call after its block to the top level of the template, or register it with `RegisterTemplate`. Otherwise those calls throw `Referenced partial name '…' could not be resolved`, or produce `MissingPartialTemplateHandler` output if a handler is configured. + +##### Areas +- `Compile-time` + +##### Example +```c# +[Fact] +public void BlockScopedInlinePartials() +{ + var handlebars = Handlebars.Create(); + handlebars.Configuration.Compatibility.BlockScopedInlinePartials = true; + + var template = handlebars.Compile( + "{{#*inline \"p\"}}outer{{/inline}}{{#with .}}{{#*inline \"p\"}}inner{{/inline}}{{> p}}{{/with}} {{> p}}"); + + Assert.Equal("inner outer", template(new { })); // "inner inner" with the default behavior +} +``` + #### HtmlEncoder Used to switch between the legacy Handlebars.Net and the canonical Handlebars rules (or a custom implementation).\ For Handlebars.Net 2.x.x `HtmlEncoderLegacy` is the default. diff --git a/source/Handlebars.Test/InlinePartialTests.cs b/source/Handlebars.Test/InlinePartialTests.cs index 5da7b22c..4e28f254 100644 --- a/source/Handlebars.Test/InlinePartialTests.cs +++ b/source/Handlebars.Test/InlinePartialTests.cs @@ -486,12 +486,45 @@ public void BlockInlinePartialWithInlinePartials() Assert.Equal("Hello Pete", result4); } - // https://github.com/Handlebars-Net/Handlebars.Net/issues/673 + // Compatibility.BlockScopedInlinePartials (opt-in) — https://github.com/Handlebars-Net/Handlebars.Net/issues/673 + private static IHandlebars CreateWithBlockScopedInlinePartials() + { + var handlebars = Handlebars.Create(); + handlebars.Configuration.Compatibility.BlockScopedInlinePartials = true; + return handlebars; + } + + [Fact] + public void InlinePartialInBlockStaysRegisteredAfterBlockByDefault() + { + var handlebars = Handlebars.Create(); + var template = handlebars.Compile( + "{{#*inline \"myPartial\"}}outer{{/inline}}" + + "{{#with .}}{{#*inline \"myPartial\"}}inner{{/inline}}{{> myPartial}}{{/with}} " + + "{{> myPartial}}"); + + Assert.Equal("inner inner", template(new { })); + + var after = handlebars.Compile("{{#with .}}{{#*inline \"myPartial\"}}success{{/inline}}{{/with}}{{> myPartial}}"); + Assert.Equal("success", after(new { })); + } + + [Fact] + public void InlinePartialsDeclaredInIncludedPartialRemainAvailableByDefault() + { + var handlebars = Handlebars.Create(); + handlebars.RegisterTemplate("macros", "{{#*inline \"button\"}}{{/inline}}"); + + var template = handlebars.Compile("{{> macros}}{{> button label=\"OK\"}}"); + + Assert.Equal("", template(new { })); + } + // handlebars.js spec/partials.js: "should define inline partials for block" [Fact] public void InlinePartialIsScopedToDeclaringBlock() { - var handlebars = Handlebars.Create(); + var handlebars = CreateWithBlockScopedInlinePartials(); var inside = handlebars.Compile("{{#with .}}{{#*inline \"myPartial\"}}success{{/inline}}{{> myPartial}}{{/with}}"); Assert.Equal("success", inside(new { })); @@ -504,7 +537,7 @@ public void InlinePartialIsScopedToDeclaringBlock() [Fact] public void InlinePartialInBlockOverridesOuterOnlyInsideBlock() { - var handlebars = Handlebars.Create(); + var handlebars = CreateWithBlockScopedInlinePartials(); var template = handlebars.Compile( "{{#*inline \"myPartial\"}}outer{{/inline}}" + "{{#with .}}{{#*inline \"myPartial\"}}inner{{/inline}}{{> myPartial}}{{/with}} " + @@ -516,7 +549,7 @@ public void InlinePartialInBlockOverridesOuterOnlyInsideBlock() [Fact] public void InlinePartialInBlockIsVisibleToNestedBlocks() { - var handlebars = Handlebars.Create(); + var handlebars = CreateWithBlockScopedInlinePartials(); var template = handlebars.Compile( "{{#with .}}{{#*inline \"myPartial\"}}success{{/inline}}" + "{{#with .}}{{#if true}}{{> myPartial}}{{/if}}{{/with}}{{/with}}"); @@ -527,7 +560,7 @@ public void InlinePartialInBlockIsVisibleToNestedBlocks() [Fact] public void InlinePartialInEachIsScopedToLoop() { - var handlebars = Handlebars.Create(); + var handlebars = CreateWithBlockScopedInlinePartials(); var template = handlebars.Compile( "{{#*inline \"item\"}}outer{{/inline}}" + "{{#each items}}{{#*inline \"item\"}}[{{.}}]{{/inline}}{{> item}}{{/each}} {{> item}}"); @@ -538,7 +571,7 @@ public void InlinePartialInEachIsScopedToLoop() [Fact] public void LastInlinePartialDefinitionInBlockWins() { - var handlebars = Handlebars.Create(); + var handlebars = CreateWithBlockScopedInlinePartials(); var template = handlebars.Compile( "{{#with .}}{{#*inline \"p\"}}first{{/inline}}{{#*inline \"p\"}}second{{/inline}}{{> p}}{{/with}}"); @@ -548,7 +581,7 @@ public void LastInlinePartialDefinitionInBlockWins() [Fact] public void InlinePartialsInBlockWithCustomDecoratorAreScopedToBlock() { - var handlebars = Handlebars.Create(); + var handlebars = CreateWithBlockScopedInlinePartials(); handlebars.RegisterDecorator("noop", (TemplateDelegate function, in DecoratorOptions options, in Context context, in Arguments arguments) => function); @@ -566,7 +599,7 @@ public void InlinePartialsInBlockWithCustomDecoratorAreScopedToBlock() [Fact] public void InlinePartialInPartialBlockIsScopedToPartialBlock() { - var handlebars = Handlebars.Create(); + var handlebars = CreateWithBlockScopedInlinePartials(); handlebars.RegisterTemplate("layout", "<{{> content}}>"); var template = handlebars.Compile( @@ -582,7 +615,7 @@ public void InlinePartialInPartialBlockIsScopedToPartialBlock() [Fact] public void InlinePartialScopeIsRestoredOnRepeatedRenders() { - var handlebars = Handlebars.Create(); + var handlebars = CreateWithBlockScopedInlinePartials(); var template = handlebars.Compile( "{{#*inline \"p\"}}outer{{/inline}}{{#with .}}{{#*inline \"p\"}}inner{{/inline}}{{> p}}{{/with}}{{> p}}"); diff --git a/source/Handlebars/Compiler/Translation/Expression/DecoratorDefinition.cs b/source/Handlebars/Compiler/Translation/Expression/DecoratorDefinition.cs index d8e9c32e..6c8722e2 100644 --- a/source/Handlebars/Compiler/Translation/Expression/DecoratorDefinition.cs +++ b/source/Handlebars/Compiler/Translation/Expression/DecoratorDefinition.cs @@ -51,6 +51,8 @@ CompilationContext context f(writer, bindingContext, current(writer, bindingContext, function)); } + if (!context.Configuration.Compatibility.BlockScopedInlinePartials) return decorator; + // Inline partials ({{#*inline}}) declared by a block's decorators are only visible // while that block renders (handlebars.js semantics): open a partials scope on the // frame before the decorators run and restore the previous set once the decorated diff --git a/source/Handlebars/Configuration/Compatibility.cs b/source/Handlebars/Configuration/Compatibility.cs index 0b67ab6b..76a6a95d 100644 --- a/source/Handlebars/Configuration/Compatibility.cs +++ b/source/Handlebars/Configuration/Compatibility.cs @@ -31,5 +31,16 @@ public class Compatibility /// Intended only as a temporary escape hatch for templates that depend on the old behavior. /// public bool LegacyStandaloneCaretBehavior { get; set; } = false; + + /// + /// If limits an inline partial ({{#*inline}}) declared inside a block to that block, as in Handlebarsjs. + /// The partial is visible in the declaring block and its children only; once the block has rendered it can no longer be + /// called, and an outer partial with the same name becomes visible again: + /// {{#*inline "p"}}outer{{/inline}}{{#with .}}{{#*inline "p"}}inner{{/inline}}{{> p}}{{/with}} {{> p}} renders inner outer. + /// By default () the earlier Handlebars.Net behavior is kept: inline partials declared inside a block + /// (or inside a partial) stay registered on the enclosing scope after it ends, so the template above renders inner inner. + /// Templates that call a block's inline partial after the block will throw "could not be resolved" when this is enabled. + /// + public bool BlockScopedInlinePartials { get; set; } = false; } } \ No newline at end of file