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 51c415a6..4e28f254 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,143 @@ public void BlockInlinePartialWithInlinePartials() var result4 = template(data); Assert.Equal("Hello Pete", result4); } + + // 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 = CreateWithBlockScopedInlinePartials(); + + 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 = CreateWithBlockScopedInlinePartials(); + 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 = CreateWithBlockScopedInlinePartials(); + 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 = CreateWithBlockScopedInlinePartials(); + 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 = CreateWithBlockScopedInlinePartials(); + 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 = CreateWithBlockScopedInlinePartials(); + 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 = CreateWithBlockScopedInlinePartials(); + 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 = CreateWithBlockScopedInlinePartials(); + 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..6c8722e2 100644 --- a/source/Handlebars/Compiler/Translation/Expression/DecoratorDefinition.cs +++ b/source/Handlebars/Compiler/Translation/Expression/DecoratorDefinition.cs @@ -51,7 +51,40 @@ CompilationContext context f(writer, bindingContext, current(writer, bindingContext, function)); } - return decorator; + 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 + // 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 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