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 @@ -734,6 +734,23 @@ public void UnrecognisedExpressionThrowsOutOfMemoryException()
Assert.Throws<HandlebarsCompilerException>(()=> Handlebars.Compile(source));
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/519
[Fact]
public void Issue519_PartialBlockUsableAsBlockAndInIf()
{
var handlebars = Handlebars.Create();
handlebars.RegisterTemplate("myPartial",
@"Conditional:{{#if @partial-block}} {{> @partial-block}}{{/if}}
Plain: {{> @partial-block}}
Block:{{#> @partial-block }}{{/@partial-block}}");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using

Block:{{#> @partial-block }}any fallback{{/@partial-block}}

should still let the test pass, but doesn't, right? I am a bit stuck in figuring out the right solution for this, so I can continue with #606


var render = handlebars.Compile("{{#> myPartial}}Block content{{/myPartial}}");
var actual = render(new { });
Assert.Contains("Conditional: Block content", actual);
Assert.Contains("Plain: Block content", actual);
Assert.Contains("Block:Block content", actual);
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/458
// System.NotImplementedException: byref delegate on Xamarin.iOS / Mono
// The Mono runtime does not support delegates with byref (in/ref) parameters.
Expand Down
15 changes: 14 additions & 1 deletion source/Handlebars/BindingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,20 @@ internal bool TryGetContextVariable(ChainSegment segment, out object value)
return BlockParamsObject.TryGetValue(segment, out value)
|| ContextDataObject.TryGetValue(wellKnownVariable, out value);
}


// Special handling for @partial-block: return the PartialBlockTemplate delegate
// so that {{#if @partial-block}} is truthy when a block partial is provided.
if (segment.LowerInvariant == "partial-block")
{
if (PartialBlockTemplate != null)
{
value = PartialBlockTemplate;
return true;
}
value = UndefinedBindingResult.Create(segment.TrimmedValue);
return false;
}

return BlockParamsObject.TryGetValue(segment, out value)
|| ContextDataObject.TryGetValue(segment, out value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@

public override Expression GetAccumulatedBlock()
{
Expression fallback = _body.Count == 0
? null
: _body.Count == 1
? _body.First()
: Expression.Block(_body);

Check warning on line 31 in source/Handlebars/Compiler/Lexer/Converter/BlockAccumulators/PartialBlockAccumulatorContext.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Extract this nested ternary operation into an independent statement.

See more on https://sonarcloud.io/project/issues?id=Handlebars-Net_Handlebars.Net&issues=AZ7ii6dB4ZKJmrimQf-s&open=AZ7ii6dB4ZKJmrimQf-s&pullRequest=624

return HandlebarsExpression.Partial(
_startingNode.PartialName,
_startingNode.Argument,
_body.Count > 1 ? Expression.Block(_body) : _body.First());
fallback);
}

public override bool IsClosingElement(Expression item)
Expand Down
Loading