Repository navigation
Description
Follow-up to #683, #687 and #684. This is a deliberate breaking change for the next release.
Current behaviour (master, after #687)
Under {{#if}} / {{#unless}}, an {{else name}} with nothing after the name is still treated as a plain {{else}}: name is ignored. This was kept in #687 for backward compatibility. Every other chained form ({{else if x}}, {{else unless x}}, {{else helper a b}}) already follows handlebars.js.
In handlebars.js, {{else name}} is {{else}}{{#name}}…{{/name}}. That means:
- a registered block helper
nameis invoked; - otherwise
nameis resolved as a value (block-helper-missing), so the branch renders only if it is truthy, and iterates if it is an array.
A bare {{else if}} / {{else unless}} with no condition is an error ("#if requires exactly one argument").
Requested change
- Default: handlebars.js semantics for bare
{{else name}}under{{#if}}/{{#unless}}. Route it through the same nested-block desugaring feat: support else-if chaining for block helpers #648 / fix: chain {{else helper args}} off {{#if}}/{{#unless}} blocks #687 use for other chainedelseforms. A bare{{else if}}/{{else unless}}becomes aHandlebarsCompilerException. - Opt-out: a new
Configuration.Compatibilityflag restores today's behaviour (bare name ignored, renders as plain{{else}}). It is read at compile time. It should follow the existingLegacyStandaloneCaretBehaviorconvention (and Make compatibility fixes opt-out: block-scoped inline partials by default, opt-outs named in exceptions #692): the name starts withLegacy, defaultfalse, andtruemeans old behaviour. Suggested name:LegacyBareElseNameBehavior. - Error message: when the new default throws for a bare
{{else if}}/{{else unless}}, the message names the opt-out. For example: "#if requires exactly one argument. To restore the previous behavior, where a bare '{{else if}}' rendered as '{{else}}', set Configuration.Compatibility.LegacyBareElseNameBehavior = true". - Under non-conditional block helpers (
{{#each}}, custom block helpers) bare{{else name}}already desugars since feat: support else-if chaining for block helpers #648. Leave that unchanged in both modes. - A non-path first argument (e.g.
{{else "x" b}}) currently keeps the legacy "else if on the second argument" behaviour. Decide whether the new default matches handlebars.js there too. Check handlebars.js 4.7.8 output first, and gate it behind the same flag if it changes.
What changes for existing users
Actual output from master (#687) vs. #684's implementation, which already has the requested default semantics:
| Template | Data | master (= opt-out) | New default |
|---|---|---|---|
{{#if a}}A{{else isAdmin}}B{{/if}} |
a=false, isAdmin=false | B |
`` |
{{#if a}}A{{else items}}[{{this}}]{{/if}} |
a=false, items=[1,2] | [{ a = False, … }] |
[1][2] |
{{#if a}}A{{else if}}B{{/if}} |
a=false | B |
HandlebarsCompilerException |
Realistic case: {{#if user.isAdmin}}Admin tools{{else user.isModerator}}Moderator tools{{/if}} (the if was left out). Today every non-admin sees "Moderator tools". With the new default, only moderators do.
Implementation notes and credit
#684 by @EnesKartal already implements the default semantics. Its IsElseIfBlock only accepts if/unless by name, and every other {{else name …}}, including bare names, is left to BlockAccumulator. Port that logic behind the flag. Keep #687's handling of {{else}} after {{else}} (HandlebarsCompilerException), which #684 lacks. Credit @EnesKartal with a Co-authored-by trailer on the implementing commit, and in the release notes' Contributors line.
Acceptance
- Tests in
ConditionalElseChainTestsfor both modes: each table row above, plus{{else unless}}, with#ifand#unlessas the outer block. ExceptionTestsrow checking that the message names the opt-out.- README / compatibility docs list the new flag.
- Release: this is breaking, so give it a major version bump unless it ships together with Make compatibility fixes opt-out: block-scoped inline partials by default, opt-outs named in exceptions #692's opt-out policy release. Release notes must list the flag under Compatibility notes with the table above.
Activity
stencil-harness commentedon Nov 22, 2026
I've opened #696 for review. It changes only Handlebars-Net/Handlebars.Net, and the work is still on
robot/issue-695: nothing is merged or released.What changed
{{#if}}/{{#unless}}, a bare{{else name}}now works as it does in handlebars.js, as if it were{{else}}{{#name}}…{{/name}}. A registered block helper callednameis run. Otherwisenameis treated as a value: the branch renders only when it is truthy, and repeats for each item when it is a list. This ports fix: chain {{else helper ...}} under {{#if}} and {{#unless}} #684's logic, and @EnesKartal is credited as co-author on the commit.{{else if}}or{{else unless}}now throwsHandlebarsCompilerException. The message names the opt-out, e.g. "#if requires exactly one argument. To restore the previous behavior, where a bare '{{else if}}' rendered as '{{else}}', set Configuration.Compatibility.LegacyBareElseNameBehavior = true".Compatibility.LegacyBareElseNameBehavior(defaultfalse, read at compile time) brings back the old behaviour, where the name is ignored and the branch acts as a plain{{else}}.{{else name}}under{{#each}}or a custom block helper;{{else}}after{{else}}, which is still a compiler error;{{else "x" b}}.{{else "x" b}}decision: I kept the existing Handlebars.Net behaviour for it. My reading of the handlebars.js 4.7.8 grammar is that it calls a helper namedxor fails withMissing helper: "x". I didn't run handlebars.js itself to confirm this. Matching it would need literal-named block helpers, which is a separate feature, so I left it out of this change.How I checked it
ConditionalElseChainTestscover both modes: every row of the issue's table, plus{{else unless}}, with#ifand#unlessas the outer block.ExceptionTestschecks the full error message.MERGEABLE. Its merge state isBLOCKED, which normally means it is waiting on required checks or review.What you need to decide
LegacyBareElseNameBehaviorunder Compatibility notes with the table from the PR, and add @EnesKartal to the Contributors line.ConditionalBlockAccumulatorContext.cs,Compatibility.csand the README. Whichever PR merges second will need a small merge.stencil-harness commentedon Nov 22, 2026
I merged
masterinto #696 and resolved the conflict with #692 inCompatibility.cs, keeping both changes. The full test suite passes (2326 passed, 0 failed), and GitHub reports the PR asMERGEABLE. Only Handlebars-Net/Handlebars.Net changed. The change is still onrobot/issue-695for review and is not yet merged or released.feat!: handlebars.js semantics for bare {{else name}} under #if/#unless