Skip to content

Use handlebars.js semantics for a bare {{else name}} under {{#if}}/{{#unless}}, with LegacyBareElseNameBehavior opt-out - #696

Merged
rexm merged 2 commits into
masterfrom
robot/issue-695
Nov 22, 2026
Merged

rexm merged 2 commits into
masterfrom
robot/issue-695

Conversation

@stencil-harness

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

Copy link
Copy Markdown
Contributor

Closes #695. Follow-up to #683 / #687, porting the default semantics from #684 by @EnesKartal (credited with a Co-authored-by trailer on the commit).

This is a breaking change. See "Release" below.

What changes

Under {{#if}} / {{#unless}}, a bare {{else name}} (nothing after the name) now works as it does in handlebars.js: it means {{else}}{{#name}}…{{/name}}. It goes through the same nested-block desugaring that #648 / #687 use for the other chained else forms:

  • if name is a registered block helper, that helper is invoked;
  • otherwise name is resolved as a value, so the branch renders only when it is truthy, and iterates when it is a list.

A bare {{else if}} / {{else unless}} with no condition now throws HandlebarsCompilerException. The message names the opt-out:

#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: Configuration.Compatibility.LegacyBareElseNameBehavior

  • Default false. Set it to true to get the previous behaviour back: the name is ignored and the branch renders as a plain {{else}}.
  • It is read at compile time and follows the LegacyStandaloneCaretBehavior naming convention.

Not changed (in either mode)

What changes for existing users

Each row below is asserted by a test in ConditionalElseChainTests.

Template Data Before (= LegacyBareElseNameBehavior = true) New default
{{#if a}}A{{else isAdmin}}B{{/if}} a=false, isAdmin=false B (empty)
{{#if a}}A{{else items}}[{{this}}]{{/if}} a=false, items=[1,2] [ + outer context + ] [1][2]
{{#if a}}A{{else if}}B{{/if}} a=false B HandlebarsCompilerException
{{#if a}}A{{else unless}}B{{/if}} a=false B HandlebarsCompilerException

Realistic case, where the if was left out: {{#if user.isAdmin}}Admin tools{{else user.isModerator}}Moderator tools{{/if}}. Before this change, every non-admin saw "Moderator tools". Now only moderators do.

Tests

  • ConditionalElseChainTests covers both modes:
    • a bare name resolved as a value, with #if and #unless as the outer block;
    • iterating a list;
    • invoking a registered block helper;
    • bare {{else if}} / {{else unless}} under both outer blocks;
    • {{else}} after {{else}};
    • the admin/moderator case;
    • {{#each}} unaffected;
    • the non-path first argument unaffected.
  • ExceptionTests.TestConditionlessElseIfExceptionNamesOptOut checks the full message for all four bare forms.
  • The full suite dotnet test source/Handlebars.Test/Handlebars.Test.csproj passes after merging master: 2326 passed, 0 failed.

Docs

The README's "Compatibility feature toggles" section has a new LegacyBareElseNameBehavior entry, with the table and an example. The flag also has XML docs in Compatibility.cs.

Release

Steps for the maintainer:

  1. Review and merge this PR.
  2. Make compatibility fixes opt-out: block-scoped inline partials by default, opt-outs named in exceptions #692 has merged, and this branch now includes master with the conflict in Compatibility.cs resolved. It keeps Make compatibility fixes opt-out: block-scoped inline partials by default, opt-outs named in exceptions #692's BlockScopedInlinePartials = true default and adds LegacyBareElseNameBehavior. The full suite passes after the merge: 2326 passed, 0 failed.
  3. Publish a GitHub Release with the chosen tag (no v prefix), following the release process in AGENTS.md.

🤖 Generated with Claude Code

Closes #695

A bare "{{else name}}" inside {{#if}}/{{#unless}} now desugars to
"{{else}}{{#name}}...{{/name}}" as in handlebars.js: a registered block
helper is invoked, otherwise the name is resolved as a value (renders
only when truthy, iterates lists). A condition-less "{{else if}}" /
"{{else unless}}" is a HandlebarsCompilerException whose message names
the opt-out.

Compatibility.LegacyBareElseNameBehavior = true restores the previous
behavior (name ignored, plain {{else}}). Non-conditional blocks and a
non-path first argument ("{{else "x" b}}") are unchanged in both modes.

Refs #695

Co-authored-by: EnesKartal <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rexm

rexm commented Nov 22, 2026

Copy link
Copy Markdown
Member

Resolve merge conflicts

# Conflicts:
#	source/Handlebars/Configuration/Compatibility.cs
@stencil-harness

Copy link
Copy Markdown
Contributor Author

I merged master into robot/issue-695 and resolved the conflict. It was only in Compatibility.cs, where #692 changed the BlockScopedInlinePartials default to true. I kept that and added LegacyBareElseNameBehavior. Git merged ConditionalBlockAccumulatorContext.cs and the README without conflicts, and #692's standalone-caret opt-out message is kept alongside the new bare-{{else if}} check.

The full test suite passes: 2326 passed, 0 failed. GitHub now reports the PR as MERGEABLE, with merge state BLOCKED, which normally means waiting on required checks or review. I updated the PR description to match.

@sonarqubecloud

Copy link
Copy Markdown

@rexm
rexm enabled auto-merge November 22, 2026 20:29
@rexm
rexm merged commit 92a86d0 into master Nov 22, 2026
7 checks passed
@rexm
rexm deleted the robot/issue-695 branch November 22, 2026 20:29
@stencil-harness stencil-harness Bot mentioned this pull request Nov 22, 2026
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.

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

1 participant