Skip to content

fix: chain {{else helper args}} off {{#if}}/{{#unless}} blocks - #687

Merged
stencil-harness[bot] merged 2 commits into
masterfrom
robot/issue-683
Nov 21, 2026
Merged

stencil-harness[bot] merged 2 commits into
masterfrom
robot/issue-683

Conversation

@stencil-harness

@stencil-harness stencil-harness Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #683.

Problem

Inside {{#if}} / {{#unless}}, {{else name args}} was only recognised with exactly two arguments, and was then always evaluated as {{else if x}}, whatever name was. Any other shape was treated as a plain {{else}}, so the condition was silently dropped. If a later {{else}} followed, Compile threw ArgumentNullException (Parameter 'test'), which is not a HandlebarsException.

Change

  • ConditionalBlockAccumulatorContext now flattens only {{else if x}} (with optional hash) into its internal condition chain. Every other named chain ({{else unless x}}, {{else customHelper a b}}) goes through the generic nested-block desugaring added in feat: support else-if chaining for block helpers #648. That makes it equivalent to {{else}}{{#name args}}…{{/name}}, which is what handlebars.js does.
  • HandlesChainedElseInternally is now a per-else method rather than a per-block flag, so a context can choose for each else.
  • The final branch of a conditional is now closed when the block is accumulated, not only at its literal closing tag. That closing tag is consumed by the nested chained block.
  • A second {{else}} after a plain {{else}}, and {{else if a b}} / {{else unless a b}}, now throw HandlebarsCompilerException with the template location, instead of ArgumentNullException or rendering silently.
  • {{else name}} with nothing after the name (including a bare {{else if}}) keeps its existing behaviour: it renders as a plain {{else}}.

Before / after (actual runs, ifEq / ifNot are registered block helpers)

Template Data Before After
{{#if a}}A{{else ifEq b "x"}}B{{else}}C{{/if}} a=false, b="x" ArgumentNullException B
{{#if a}}A{{else ifEq b "y"}}B{{/if}} a=false, b="x" B (wrong) ``
{{#if a}}A{{else ifNot b}}B{{else}}C{{/if}} a=false, b=false C (ifNot ignored, used as else if b) B
{{#if a}}A{{else unless b}}B{{else}}C{{/if}} a=false, b=false C (treated as else if b) B
{{#if a}}A{{else if b includeZero=true}}B{{else}}C{{/if}} a=false, b=0 ArgumentNullException B
{{#if a}}A{{else if b c}}B{{/if}} a=false B HandlebarsCompilerException: #if requires exactly one argument
{{#if a}}A{{else}}B{{else}}C{{/if}} a=false ArgumentNullException HandlebarsCompilerException
{{#if a}}A{{else missingHelper b}}B{{/if}} a=false, b=true B HandlebarsRuntimeException: … BlockHelper '#missingHelper'

Compatibility notes

Every changed row was wrong before: the output differed from handlebars.js, or compilation failed with a non-Handlebars exception. Behaviour changes that existing templates could notice:

  • {{else unless x}} under {{#if}} was evaluated as {{else if x}}. The branch condition was inverted. It now has unless semantics.
  • {{else someHelper x}} with one argument after the name used x as an else if condition and ignored someHelper. It now invokes someHelper. If no such helper is registered, rendering throws the same missing-helper HandlebarsRuntimeException you get for {{#someHelper x}}. handlebars.js also fails here.
  • {{else if a b}} / {{else unless a b}} (too many arguments) now fail at compile time instead of always rendering their branch.

Plain {{else}}, {{else if x}} and {{else name}} are unchanged. Chaining under non-conditional block helpers (#648) is unchanged.

Suggested release: patch. These are defect fixes toward handlebars.js behaviour, and no API changes.

Relationship to #684

#684 (by @EnesKartal) fixes the same issue with the same core approach: a per-else hook, routing non-if/unless chains through the #648 desugaring, and building the last branch in AccumulatedBlock. This PR now also includes #684's wrong-argument-count cases for {{else if}} / {{else unless}} as ExceptionTests rows. Differences, based on #684's description:

Verification

  • source/Handlebars.Test/ConditionalElseChainTests.cs covers: custom helper chains with and without a trailing {{else}}; #unless as the outer block; a single-argument custom helper; else unless under both #if and #unless; mixed else if / custom-helper chains in both orders; else if with a hash argument; whitespace control; the compiler-error cases; and the unchanged bare-name {{else …}} behaviour.
  • ExceptionTests.TestConditionalBlockWithWrongArgumentCountException has new rows for malformed {{else if}} / {{else unless}} under both #if and #unless.
  • Full suite: dotnet test source/Handlebars.Test/Handlebars.Test.csproj: 2270 passed, 0 failed. Library builds for all target frameworks.

🤖 Generated with Claude Code

Closes #683

{{else name args}} under an if/unless block was only understood when it had
exactly two arguments, and then always as {{else if x}}. Anything else was
treated as a plain {{else}} (dropping the condition) and a following {{else}}
threw ArgumentNullException at compile time.

The conditional block now resolves only {{else if x}} internally; other named
chains ({{else unless x}}, {{else customHelper a b}}) use the generic nested
block desugaring, matching handlebars.js. A second {{else}} after a plain
{{else}} and a malformed {{else if}} now raise HandlebarsCompilerException.

Fixes #683

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Adds the wrong-argument-count cases for {{else if}} / {{else unless}} that #684
covers, plus else-unless under #unless, whitespace control around a chained
custom helper, and the unchanged plain-else behaviour of a bare {{else if}}.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sonarqubecloud

Copy link
Copy Markdown

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.

{{else customHelper args}} inside {{#if}} / {{#unless}} throws ArgumentNullException (Parameter 'test') at compile time

0 participants