Repository navigation
fix: chain {{else helper args}} off {{#if}}/{{#unless}} blocks - #687
Merged
Merged
Conversation
{{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]>
|
This was referenced Nov 21, 2026
Closed
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 #683.
Problem
Inside
{{#if}}/{{#unless}},{{else name args}}was only recognised with exactly two arguments, and was then always evaluated as{{else if x}}, whatevernamewas. Any other shape was treated as a plain{{else}}, so the condition was silently dropped. If a later{{else}}followed,CompilethrewArgumentNullException (Parameter 'test'), which is not aHandlebarsException.Change
ConditionalBlockAccumulatorContextnow 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.HandlesChainedElseInternallyis now a per-elsemethod rather than a per-block flag, so a context can choose for eachelse.{{else}}after a plain{{else}}, and{{else if a b}}/{{else unless a b}}, now throwHandlebarsCompilerExceptionwith the template location, instead ofArgumentNullExceptionor 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/ifNotare registered block helpers){{#if a}}A{{else ifEq b "x"}}B{{else}}C{{/if}}ArgumentNullExceptionB{{#if a}}A{{else ifEq b "y"}}B{{/if}}B(wrong){{#if a}}A{{else ifNot b}}B{{else}}C{{/if}}C(ifNotignored, used aselse if b)B{{#if a}}A{{else unless b}}B{{else}}C{{/if}}C(treated aselse if b)B{{#if a}}A{{else if b includeZero=true}}B{{else}}C{{/if}}ArgumentNullExceptionB{{#if a}}A{{else if b c}}B{{/if}}BHandlebarsCompilerException: #if requires exactly one argument{{#if a}}A{{else}}B{{else}}C{{/if}}ArgumentNullExceptionHandlebarsCompilerException{{#if a}}A{{else missingHelper b}}B{{/if}}BHandlebarsRuntimeException: … 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 hasunlesssemantics.{{else someHelper x}}with one argument after the name usedxas anelse ifcondition and ignoredsomeHelper. It now invokessomeHelper. If no such helper is registered, rendering throws the same missing-helperHandlebarsRuntimeExceptionyou 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-
elsehook, routing non-if/unlesschains through the #648 desugaring, and building the last branch inAccumulatedBlock. This PR now also includes #684's wrong-argument-count cases for{{else if}}/{{else unless}}asExceptionTestsrows. Differences, based on #684's description:{{else unless x}}: fix: chain {{else helper ...}} under {{#if}} and {{#unless}} #684 negates it inside the flat chain; this PR desugars it to a nested{{#unless}}. Same output, and the same#unless requires exactly one argumenterror for bad arity.{{else}}followed by another{{else}}: this PR raisesHandlebarsCompilerException. fix: chain {{else helper ...}} under {{#if}} and {{#unless}} #684 does not mention this case, which threwArgumentNullException.{{else if}}/{{else unless}}with no condition: this PR keeps rendering it as a plain{{else}}, which is today's behaviour, so existing templates keep compiling. fix: chain {{else helper ...}} under {{#if}} and {{#unless}} #684 describes a wrong argument count as a compile error.ConditionalElseChainTests.cs, per AGENTS.md. fix: chain {{else helper ...}} under {{#if}} and {{#unless}} #684 adds them toIssueTests.Verification
source/Handlebars.Test/ConditionalElseChainTests.cscovers: custom helper chains with and without a trailing{{else}};#unlessas the outer block; a single-argument custom helper;else unlessunder both#ifand#unless; mixedelse if/ custom-helper chains in both orders;else ifwith a hash argument; whitespace control; the compiler-error cases; and the unchanged bare-name{{else …}}behaviour.ExceptionTests.TestConditionalBlockWithWrongArgumentCountExceptionhas new rows for malformed{{else if}}/{{else unless}}under both#ifand#unless.dotnet test source/Handlebars.Test/Handlebars.Test.csproj: 2270 passed, 0 failed. Library builds for all target frameworks.🤖 Generated with Claude Code
Closes #683