Skip to content

Commit e6e4d53

Browse files
authored
Merge pull request #516 from /issues/515
Nested partial block receives correct context
2 parents e709aa9 + e68a2c8 commit e6e4d53

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

source/Handlebars.Test/IssueTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,23 @@ End outer partial block<br />
161161
Assert.Equal(expected, result);
162162
}
163163

164+
// issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/515
165+
[Fact]
166+
public void ValidContextInNestedPartialBlock()
167+
{
168+
const string template = @"{{#> [a/b] c=this }}{{c.value}}{{/ [a/b] }}";
169+
const string partial = "{{c.value}} {{> @partial-block }}";
170+
171+
var handlebars = Handlebars.Create();
172+
handlebars.RegisterTemplate("a/b", @partial);
173+
174+
var callback = handlebars.Compile(template);
175+
var result = callback(new { value = 42 });
176+
177+
const string expected = @"42 42";
178+
Assert.Equal(expected, result);
179+
}
180+
164181
// issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/395
165182
[Fact]
166183
public void RenderingWithUnusedPartial()

source/Handlebars/BindingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ out WellKnownVariables[(int) WellKnownVariable.Parent]
115115

116116
internal CascadeIndex<string, IHelperDescriptor<BlockHelperOptions>, StringEqualityComparer> BlockHelpers { get; }
117117

118-
internal TemplateDelegate PartialBlockTemplate { get; private set; }
118+
internal TemplateDelegate PartialBlockTemplate { get; set; }
119119

120120
public object Value { get; set; }
121121

source/Handlebars/Compiler/Translation/Expression/PartialBinder.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq.Expressions;
44
using Expressions.Shortcuts;
5+
using HandlebarsDotNet.PathStructure;
56
using HandlebarsDotNet.Polyfills;
67
using static Expressions.Shortcuts.ExpressionShortcuts;
78

@@ -92,6 +93,7 @@ private static void InvokePartialWithFallback(
9293
EncodedTextWriter writer,
9394
ICompiledHandlebarsConfiguration configuration)
9495
{
96+
partialName = partialName != null ? ChainSegment.Create(partialName).TrimmedValue : null;
9597
if (InvokePartial(partialName, context, writer, configuration)) return;
9698
if (context.PartialBlockTemplate == null)
9799
{
@@ -118,7 +120,16 @@ private static bool InvokePartial(
118120
return false;
119121
}
120122

121-
context.PartialBlockTemplate(writer, context.ParentContext);
123+
var partialBlockTemplate = context.PartialBlockTemplate;
124+
try
125+
{
126+
context.PartialBlockTemplate = context.ParentContext.PartialBlockTemplate;
127+
partialBlockTemplate(writer, context);
128+
}
129+
finally
130+
{
131+
context.PartialBlockTemplate = partialBlockTemplate;
132+
}
122133
return true;
123134
}
124135

0 commit comments

Comments
 (0)