Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions source/Handlebars.Test/IssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ End outer partial block<br />
Assert.Equal(expected, result);
}

// issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/515
[Fact]
public void ValidContextInNestedPartialBlock()
{
const string template = @"{{#> [a/b] c=this }}{{c.value}}{{/ [a/b] }}";
const string partial = "{{c.value}} {{> @partial-block }}";

var handlebars = Handlebars.Create();
handlebars.RegisterTemplate("a/b", @partial);

var callback = handlebars.Compile(template);
var result = callback(new { value = 42 });

const string expected = @"42 42";
Assert.Equal(expected, result);
}

// issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/395
[Fact]
public void RenderingWithUnusedPartial()
Expand Down
2 changes: 1 addition & 1 deletion source/Handlebars/BindingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ out WellKnownVariables[(int) WellKnownVariable.Parent]

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

internal TemplateDelegate PartialBlockTemplate { get; private set; }
internal TemplateDelegate PartialBlockTemplate { get; set; }

public object Value { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using Expressions.Shortcuts;
using HandlebarsDotNet.PathStructure;
using HandlebarsDotNet.Polyfills;
using static Expressions.Shortcuts.ExpressionShortcuts;

Expand Down Expand Up @@ -92,6 +93,7 @@ private static void InvokePartialWithFallback(
EncodedTextWriter writer,
ICompiledHandlebarsConfiguration configuration)
{
partialName = partialName != null ? ChainSegment.Create(partialName).TrimmedValue : null;
if (InvokePartial(partialName, context, writer, configuration)) return;
if (context.PartialBlockTemplate == null)
{
Expand All @@ -118,7 +120,16 @@ private static bool InvokePartial(
return false;
}

context.PartialBlockTemplate(writer, context.ParentContext);
var partialBlockTemplate = context.PartialBlockTemplate;
try
{
context.PartialBlockTemplate = context.ParentContext.PartialBlockTemplate;
partialBlockTemplate(writer, context);
}
finally
{
context.PartialBlockTemplate = partialBlockTemplate;
}
return true;
}

Expand Down