Skip to content

fix: @partial-block usable in #if and as block partial (issue #519)#624

Merged
rexm merged 2 commits into
masterfrom
worktree-agent-a5918f4ed6a2be299
Jun 20, 2026
Merged

fix: @partial-block usable in #if and as block partial (issue #519)#624
rexm merged 2 commits into
masterfrom
worktree-agent-a5918f4ed6a2be299

Conversation

@rexm

@rexm rexm commented Jun 20, 2026

Copy link
Copy Markdown
Member

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-block template is in scope).
  • {{#> @partial-block }}{{/@partial-block}} no longer crashes with InvalidOperationException: Sequence contains no elements when the body between the tags is empty.

Root causes

Bug 1 — {{#if @partial-block}} always falsy:
@partial-block is a PathType.Variable, so its value is resolved via BindingContext.TryGetContextVariable. The PartialBlockTemplate is stored directly on BindingContext and was never exposed through the context data dictionary, so it always returned UndefinedBindingResult (falsy). Fixed by adding an explicit check in TryGetContextVariable for the segment name partial-block that returns the PartialBlockTemplate delegate 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 (passing null as the fallback, which is already the documented no-fallback sentinel in PartialExpression).

Test plan

  • New regression test Issue519_PartialBlockUsableAsBlockAndInIf added to IssueTests.cs — exercises {{#if @partial-block}}, {{> @partial-block}}, and {{#> @partial-block}}{{/@partial-block}} in the same partial.
  • Full test suite: 1747 tests, 0 failures.

🤖 Generated with Claude Code

rexm and others added 2 commits June 19, 2026 20:56
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>
@rexm rexm enabled auto-merge June 20, 2026 01:50
@sonarqubecloud

Copy link
Copy Markdown

@rexm rexm merged commit 57093ff into master Jun 20, 2026
7 checks passed
@rexm rexm deleted the worktree-agent-a5918f4ed6a2be299 branch June 20, 2026 02:01
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

TheConstructor added a commit to TheConstructor/Handlebars.Net that referenced this pull request Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@partial-block not working as partial block or as conditional

2 participants