Repository navigation
Description
Description
#648 added {{else name args}} chaining for block helpers (fixing #263). It intentionally leaves the existing {{#if}} / {{#unless}} chaining untouched, and that path only understands {{else if x}} / {{else unless x}}. Chaining a different block helper after {{#if}}, which handlebars.js supports, fails at compile time:
{{#if a}}A{{else ifEq b "x"}}B{{else}}C{{/if}}Repro (Handlebars.Net 2.4.3, net10.0)
using HandlebarsDotNet;
var hb = Handlebars.Create();
hb.RegisterHelper("ifEq", new HandlebarsBlockHelper((output, options, context, arguments) =>
{
if (Equals(arguments[0]?.ToString(), arguments[1]?.ToString())) options.Template(output, context);
else options.Inverse(output, context);
}));
var data = new Dictionary<string, object> { ["a"] = false, ["b"] = "x" };
// Expected "B" (handlebars.js 4.7.8 with the same helper renders "B").
Console.WriteLine(hb.Compile("{{#if a}}A{{else ifEq b \"x\"}}B{{else}}C{{/if}}")(data));Actual:
System.ArgumentNullException: Value cannot be null. (Parameter 'test')
The exception is thrown by Compile, before any data is read, and it is not a HandlebarsException. Callers that catch HandlebarsException to handle bad templates therefore miss it.
| Template | 2.1.6 | 2.4.3 | handlebars.js 4.7.8 |
|---|---|---|---|
{{#if a}}A{{else ifEq b "x"}}B{{else}}C{{/if}} |
ArgumentNullException |
ArgumentNullException |
B |
{{#ifEq b "y"}}A{{else ifEq b "x"}}B{{else}}C{{/ifEq}} |
C (wrong) |
B |
B |
{{#if a}}A{{else if (eq b "x")}}B{{else}}C{{/if}} (with an eq helper) |
B |
B |
B |
{{#if a}}A{{else ifEq b "y"}}B{{/if}} |
B (wrong) |
B (wrong) |
empty |
Cause (from reading master at f6e613b)
In Compiler/Lexer/Converter/BlockAccumulators/ConditionalBlockAccumulatorContext.cs:
HandlesChainedElseInternally => true, so the generic desugaring from feat: support else-if chaining for block helpers #648 is skipped forif/unless.IsElseIfBlocktreats anelseas a chained condition only when it has exactly two arguments (else if x).{{else ifEq b "x"}}has three, so it is handled as a plain{{else}}and_currentConditionbecomesnull. TheifEqbranch and its arguments are dropped.- At the next
{{else}},HandleElementcallsExpression.IfThen(_currentCondition!, ...)with anulltest, which throwsArgumentNullException (Parameter 'test'). With only one{{else ifEq ...}}and no final{{else}}there is no exception, but the condition is still dropped and the branch always renders. Witha = falseandb = "x",{{#if a}}A{{else ifEq b "y"}}B{{/if}}rendersBon both 2.1.6 and 2.4.3, where handlebars.js renders an empty string.
Related: GetElseIfTestExpression never checks that the helper named after else is if or unless. So a two-argument {{else someHelper x}} is evaluated as {{else if x}}, ignoring someHelper.
Suggested fix
In ConditionalBlockAccumulatorContext, chain internally only when the else names if or unless with a single condition argument (plus optional hash). For any other {{else name args...}}, desugar it into a nested block the same way #648 does for other block helpers. Otherwise, throw a HandlebarsCompilerException with the template location instead of an ArgumentNullException.
Environment
- Handlebars.Net 2.1.6 and 2.4.3 (NuGet), .NET 10, Windows
masterat f6e613b read, not run
Activity
rexm commentedon Nov 21, 2026
/harness adopt
stencil-harness commentedon Nov 21, 2026
I'm the agent managing intake of issues on Rex's behalf. I understand the request:
{{else someHelper args}}chained inside{{#if}}/{{#unless}}is dropped or throwsArgumentNullExceptionat compile time, and should instead chain the named block helper the way handlebars.js does. I'll evaluate whether I can address this autonomously.stencil-harness commentedon Nov 21, 2026
Fix is in #687, with auto-merge requested. It merges once the required checks pass.
Changed: Handlebars-Net/Handlebars.Net (only repo touched).
{{#if}}/{{#unless}}, only{{else if x}}(optionally with a hash) is still flattened internally. Every other named chain now nests the named block helper the same way feat: support else-if chaining for block helpers #648 does for other blocks. This covers{{else unless x}}and{{else customHelper a b}}, and matches handlebars.js.{{#if a}}A{{else ifEq b "x"}}B{{else}}C{{/if}}now rendersB.{{#if a}}A{{else ifEq b "y"}}B{{/if}}with b="x" now renders empty.{{else}}after a plain{{else}}, and{{else if a b}}, now throwHandlebarsCompilerExceptioninstead ofArgumentNullException.{{else unless x}}was evaluated as{{else if x}}, so its result was inverted, and it now behaves asunless.{{else someHelper x}}used to ignoresomeHelper; it now calls it, and throws the usual missing-helper error if it isn't registered. The PR description has a before/after table from actual runs.Verified: new
ConditionalElseChainTestsadded; the full suite passes (2259/2259) and the library builds for all targets.Shipped: the PR is not merged yet. No NuGet release was made. Suggested release is a patch.
rexm commentedon Nov 21, 2026
There is an existing PR linked to this issue. Compare both and find the best and most complete path.
stencil-harness commentedon Nov 21, 2026
Understood. I'll compare #684 with #687 on code, behaviour (actual runs against both) and tests, then consolidate on the more complete path and report back here.
stencil-harness commentedon Nov 21, 2026
Comparison of #684 and #687. This is based on #684's description and stated test results, plus actual runs of #687.
Same core fix. Both replace the per-block
HandlesChainedElseInternallyflag with a per-elsedecision. Both route{{else customHelper …}}through the #648 nested-block desugaring, and both build the last conditional branch inAccumulatedBlock. The issue's repro rendersBwith either.Differences
{{else unless x}}{{#unless}}, same output{{else if b c}}/{{else unless b c}}HandlebarsCompilerExceptionHandlebarsCompilerException(same message){{else}}…{{else}}ArgumentNullException)HandlebarsCompilerException{{else if}}(no condition){{else}}, as today, so existing templates keep compiling{{else if b includeZero=true}}ArgumentNullException)IssueTestsConditionalElseChainTests(per AGENTS.md)Path taken: I've consolidated on #687 and folded in #684's extra coverage: the malformed
else if/else unlessrows inExceptionTests, pluselse unlessunder#unlessand whitespace control. #687 is a superset, and the one place they diverge (a bare{{else if}}) favours backward compatibility.Changed: Handlebars-Net/Handlebars.Net only. I pushed one extra test commit to #687 and updated its description with the comparison.
Verified: full suite 2270/2270 passing; the library builds for all targets.
Shipped: #687 still has auto-merge requested, and its checks are running. It is not merged yet, and no release has been made (suggest a patch). #684 is left open for a maintainer to close.
stencil-harness commentedon Nov 21, 2026
Follow-up on the #684 / #687 comparison. This time I ran both PRs' actual code with the same templates and helpers, and ran #684's own tests against the merged #687.
Result: #687 (merged) covers everything #684 fixes. It also turns the double-
{{else}}case into aHandlebarsCompilerException; under #684 it still throwsArgumentNullException. #684 had one test scenario #687 lacked, chaining the built-in{{else with b}}. It already passes on master, and I've added it in #689 (tests only, credited to #684, auto-merge requested).One owner decision remains: a bare
{{else name}}(nothing after the name). #687 kept the pre-existing behaviour. #684 switches to handlebars.js semantics. Actual output:{{#if a}}A{{else isAdmin}}B{{/if}}B{{#if a}}A{{else items}}[{{this}}]{{/if}}[{ a = False, … }][1][2]{{#if a}}A{{else if}}B{{/if}}BHandlebarsCompilerException: #if requires exactly one argumentTemplates like
{{else isAdmin}}(anelse ifwith theifleft out) render today, but render the wrong thing. Switching silently changes their output in production, and a typo'd{{else if}}would stop compiling.Recommendation: leave master as is. If you want handlebars.js parity here, ship the
{{else name}}change on its own in a minor release with a compatibility note, and keep a bare{{else if}}/{{else unless}}as a plain{{else}}. Reply if you want me to do that. I won't change it without your go-ahead.Changed: Handlebars-Net/Handlebars.Net. #689 adds one test, and the full suite passes 2271/2271. Its checks are running; it is not merged yet. No release has been made. #684 is still open: it is now superseded by #687 apart from the decision above.
rexm commentedon Nov 22, 2026
If #684 is superseded let's close it and make sure the author is credited for their contributions. Cut a new issue that changes the behavior so the js semantics are on by default and there's an opt-out to go back to the old behavior. We'll cut a new release with that breaking change.
stencil-harness commentedon Nov 22, 2026
Done, apart from closing #684 itself.
{{else with}}test case. Thanks, @EnesKartal. Use handlebars.js semantics for a bare{{else name}}under{{#if}}/{{#unless}}by default, with aLegacy*opt-out #695 asks the implementing commit to carry aCo-authored-by: EnesKartaltrailer and the release notes to list them under Contributors.{{else name}}under{{#if}}/{{#unless}}by default, with aLegacy*opt-out #695: handlebars.js semantics for a bare{{else name}}/{{else if}}under{{#if}}/{{#unless}}become the default. AConfiguration.Compatibility.LegacyBareElseNameBehavioropt-out (defaultfalse, following the existingLegacy*convention and Make compatibility fixes opt-out: block-scoped inline partials by default, opt-outs named in exceptions #692) restores today's behaviour. The issue includes the before/after table from actual runs, the error-message wording that names the opt-out, test and doc acceptance criteria, and a release note recommendation (major, unless bundled with Make compatibility fixes opt-out: block-scoped inline partials by default, opt-outs named in exceptions #692's opt-out release).{{else with}}and malformedelse if/else unlesstest cases. The remaining behaviour your PR introduced (handlebars.js semantics for a bare{{else name}}) is tracked in Use handlebars.js semantics for a bare{{else name}}under{{#if}}/{{#unless}}by default, with aLegacy*opt-out #695, which will credit you as co-author. Closing as superseded."Changed: no code changes in this step; the only repo affected is Handlebars-Net/Handlebars.Net, where #695 was opened. Nothing shipped or released.
stencil-harness commentedon Nov 22, 2026
#684 is now closed as superseded. Everything requested on this issue is done:
{{else customHelper args}}inside{{#if}}/{{#unless}}throwsArgumentNullException (Parameter 'test')at compile time #683 fix: shipped in fix: chain {{else helper args}} off {{#if}}/{{#unless}} blocks #687 and test: cover chaining built-in {{else with}} under {{#if}} #689 (both merged). They include fix: chain {{else helper ...}} under {{#if}} and {{#unless}} #684's approach and its{{else with}}and malformedelse if/else unlesstest cases. Credit goes to @EnesKartal.LegacyBareElseNameBehavioropt-out. This is tracked in Use handlebars.js semantics for a bare{{else name}}under{{#if}}/{{#unless}}by default, with aLegacy*opt-out #695, which asks for aCo-authored-bytrailer and a Contributors line crediting @EnesKartal when it lands.Changed: Handlebars-Net/Handlebars.Net only, with no new code in this step. Nothing is pending on the
robot/issue-683branch. No release has been made. That waits for #695, per your note.fix: chain {{else helper args}} off {{#if}}/{{#unless}} blocks