Repository navigation
Use handlebars.js semantics for a bare {{else name}} under {{#if}}/{{#unless}}, with LegacyBareElseNameBehavior opt-out - #696
Conversation
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]>
|
Resolve merge conflicts |
# Conflicts: # source/Handlebars/Configuration/Compatibility.cs
|
I merged The full test suite passes: 2326 passed, 0 failed. GitHub now reports the PR as |
|



Closes #695. Follow-up to #683 / #687, porting the default semantics from #684 by @EnesKartal (credited with a
Co-authored-bytrailer 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 chainedelseforms:nameis a registered block helper, that helper is invoked;nameis 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 throwsHandlebarsCompilerException. The message names the opt-out:Opt-out:
Configuration.Compatibility.LegacyBareElseNameBehaviorfalse. Set it totrueto get the previous behaviour back: the name is ignored and the branch renders as a plain{{else}}.LegacyStandaloneCaretBehaviornaming convention.Not changed (in either mode)
{{else name}}under{{#each}}or a custom block helper. This already desugars, since feat: support else-if chaining for block helpers #648.{{else if x}},{{else unless x}},{{else helper a b}}. These already follow handlebars.js, since fix: chain {{else helper args}} off {{#if}}/{{#unless}} blocks #687.{{else}}after{{else}}is still aHandlebarsCompilerException(fix: chain {{else helper args}} off {{#if}}/{{#unless}} blocks #687's check is kept; fix: chain {{else helper ...}} under {{#if}} and {{#unless}} #684 did not have it).{{else "x" b}}, keeps the earlier Handlebars.Net behaviour: "else if" onb. In the handlebars.js 4.7.8 grammarhelperNameaccepts string and number literals, so there this means{{else}}{{#"x" b}}…{{/"x"}}: it calls helperx, or throwsMissing helper: "x". That conclusion comes from reading the grammar and compiler source; I did not run handlebars.js. Matching it would mean supporting literal-named block helpers in the desugaring, which is a separate feature. The behaviour is covered by a test (NonPathElseNameIsUnaffectedByLegacyFlag) and can be followed up separately if wanted.What changes for existing users
Each row below is asserted by a test in
ConditionalElseChainTests.LegacyBareElseNameBehavior = true){{#if a}}A{{else isAdmin}}B{{/if}}B{{#if a}}A{{else items}}[{{this}}]{{/if}}[+ outer context +][1][2]{{#if a}}A{{else if}}B{{/if}}BHandlebarsCompilerException{{#if a}}A{{else unless}}B{{/if}}BHandlebarsCompilerExceptionRealistic case, where the
ifwas 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
ConditionalElseChainTestscovers both modes:#ifand#unlessas the outer block;{{else if}}/{{else unless}}under both outer blocks;{{else}}after{{else}};{{#each}}unaffected;ExceptionTests.TestConditionlessElseIfExceptionNamesOptOutchecks the full message for all four bare forms.dotnet test source/Handlebars.Test/Handlebars.Test.csprojpasses after mergingmaster: 2326 passed, 0 failed.Docs
The README's "Compatibility feature toggles" section has a new
LegacyBareElseNameBehaviorentry, with the table and an example. The flag also has XML docs inCompatibility.cs.Release
LegacyBareElseNameBehaviorwith the table above.Steps for the maintainer:
masterwith the conflict inCompatibility.csresolved. It keeps Make compatibility fixes opt-out: block-scoped inline partials by default, opt-outs named in exceptions #692'sBlockScopedInlinePartials = truedefault and addsLegacyBareElseNameBehavior. The full suite passes after the merge: 2326 passed, 0 failed.vprefix), following the release process in AGENTS.md.🤖 Generated with Claude Code
Closes #695