fix: @partial-block usable in #if and as block partial (issue #519)#624
Merged
Conversation
Two sub-bugs fixed:
- BindingContext.TryGetContextVariable now returns the PartialBlockTemplate
delegate when the segment is 'partial-block', making {{#if @partial-block}}
truthy whenever a block partial is in scope.
- PartialBlockAccumulatorContext.GetAccumulatedBlock now handles an empty
body ({{#> @partial-block }}{{/@partial-block}}) without crashing.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
| handlebars.RegisterTemplate("myPartial", | ||
| @"Conditional:{{#if @partial-block}} {{> @partial-block}}{{/if}} | ||
| Plain: {{> @partial-block}} | ||
| Block:{{#> @partial-block }}{{/@partial-block}}"); |
Contributor
There was a problem hiding this comment.
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
TheConstructor
added a commit
to TheConstructor/Handlebars.Net
that referenced
this pull request
Jun 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #519
Summary
{{#if @partial-block}}now correctly evaluates as truthy when the partial is invoked as a block partial (i.e., when a@partial-blocktemplate is in scope).{{#> @partial-block }}{{/@partial-block}}no longer crashes withInvalidOperationException: Sequence contains no elementswhen the body between the tags is empty.Root causes
Bug 1 —
{{#if @partial-block}}always falsy:@partial-blockis aPathType.Variable, so its value is resolved viaBindingContext.TryGetContextVariable. ThePartialBlockTemplateis stored directly onBindingContextand was never exposed through the context data dictionary, so it always returnedUndefinedBindingResult(falsy). Fixed by adding an explicit check inTryGetContextVariablefor the segment namepartial-blockthat returns thePartialBlockTemplatedelegate when set.Bug 2 —
{{#> @partial-block }}{{/@partial-block}}crashes:PartialBlockAccumulatorContext.GetAccumulatedBlock()called_body.First()unconditionally, which throws when the body is empty. Fixed by handling the empty-body case (passingnullas the fallback, which is already the documented no-fallback sentinel inPartialExpression).Test plan
Issue519_PartialBlockUsableAsBlockAndInIfadded toIssueTests.cs— exercises{{#if @partial-block}},{{> @partial-block}}, and{{#> @partial-block}}{{/@partial-block}}in the same partial.🤖 Generated with Claude Code