Skip to content

Use handlebars.js semantics for a bare {{else name}} under {{#if}}/{{#unless}} by default, with a Legacy* opt-out #695

Closed

Description

@stencil-harness

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 name is invoked;
  • otherwise name is 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 chained else forms. A bare {{else if}} / {{else unless}} becomes a HandlebarsCompilerException.
  • Opt-out: a new Configuration.Compatibility flag restores today's behaviour (bare name ignored, renders as plain {{else}}). It is read at compile time. It should follow the existing LegacyStandaloneCaretBehavior convention (and Make compatibility fixes opt-out: block-scoped inline partials by default, opt-outs named in exceptions #692): the name starts with Legacy, default false, and true means 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 ConditionalElseChainTests for both modes: each table row above, plus {{else unless}}, with #if and #unless as the outer block.
  • ExceptionTests row 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

  1. stencil-harness commented on Nov 22, 2026

    @stencil-harness
    ContributorAuthor

    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

    • New default: under {{#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 called name is run. Otherwise name is 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.
    • New error: a bare {{else if}} or {{else unless}} now throws HandlebarsCompilerException. 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".
    • Opt-out: Compatibility.LegacyBareElseNameBehavior (default false, read at compile time) brings back the old behaviour, where the name is ignored and the branch acts as a plain {{else}}.
    • Unchanged in both modes:
      • {{else name}} under {{#each}} or a custom block helper;
      • {{else}} after {{else}}, which is still a compiler error;
      • a non-path first argument such as {{else "x" b}}.
    • The {{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 named x or fails with Missing 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.
    • Docs: the flag is documented in the README's compatibility toggles section, with the before/after table, and in the XML docs.

    How I checked it

    What you need to decide

  2. stencil-harness commented on Nov 22, 2026

    @stencil-harness
    ContributorAuthor

    I merged master into #696 and resolved the conflict with #692 in Compatibility.cs, keeping both changes. The full test suite passes (2326 passed, 0 failed), and GitHub reports the PR as MERGEABLE. Only Handlebars-Net/Handlebars.Net changed. The change is still on robot/issue-695 for review and is not yet merged or released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions