Repository navigation
2.5.0 brings Handlebars.Net closer to handlebars.js. It adds JsonNode support, and compile errors now report where they happened. Several long-standing differences from handlebars.js are now fixed by default. Each fix that changes output for templates that used to compile has a Configuration.Compatibility flag that restores the old behavior, and the new compile errors name that flag. All compatibility flags are read at compile time, so set them before calling Compile.
Compatibility notes
These changes can alter output, or turn a template that used to compile into an error. Read them before upgrading.
-
A standalone
{{^}}inside a block is now the inverse separator, the same as{{else}}, as in handlebars.js. This applies to#if,#unless,#each,#with, sections and custom block helpers, and to the{{^ }}and{{~^~}}forms. Inside raw blocks{{^}}is still literal text. To restore the previous behavior, setCompatibility.LegacyStandaloneCaretBehavior = true. (#675, fixes #672; #692)Template Data Before 2.5.0 {{#if condition}}yes{{^}}no{{/if}}condition=true yes{ condition = True }noyes{{#if condition}}yes{{^}}no{{/if}}condition=false (empty) no{{#each people}}{{name}}{{^}}No one{{/each}}[](empty) No oneA second
{{^}}in the same#if/#unless(e.g.{{#if x}}a{{^}}b{{^}}c{{/if}}) is now a compile error, and its message names the opt-out. -
Inline partials declared inside a block or partial are now scoped to that block, as in handlebars.js. The new
Compatibility.BlockScopedInlinePartialssetting defaults totrue. To restore the previous behavior, where such partials leaked into the enclosing scope and replaced outer partials with the same name, set it tofalse. When a partial can't be resolved because of this scoping, the error message names the opt-out. (#688, #692, fixes #673, #690)Template Before 2.5.0 {{#*inline "p"}}outer{{/inline}}{{#with .}}{{#*inline "p"}}inner{{/inline}}{{> p}}{{/with}} {{> p}}inner innerinner outer{{#with .}}{{#*inline "myPartial"}}success{{/inline}}{{/with}}{{> myPartial}}successthrows: partial could not be resolved {{> macros}}{{> button label="OK"}}, where partialmacrosdeclares{{#*inline "button"}}<button>OK</button>throws: partial could not be resolved To migrate, move such declarations to the top level of the template or register them with
RegisterTemplate. Either works with the setting on or off. -
{{else …}}chains under{{#if}}/{{#unless}}now follow handlebars.js.{{else name args}}now means{{else}}{{#name args}}…{{/name}}. Before, any two-word form was treated as{{else if x}}, and any other form silently became a plain{{else}}. (#687, #689, #696, fixes #683, #695; based on #684 by @EnesKartal){{else unless x}}now hasunlesssemantics. Before, it was evaluated as{{else if x}}, which inverted the condition.{{else someHelper x}}now invokessomeHelper. Before, it ignored the helper and usedxas the condition. If the helper isn't registered, rendering throws the usual missing-helperHandlebarsRuntimeException.- A bare
{{else name}}now opens a block onname. Ifnameis a registered block helper it is invoked. Otherwise the branch renders only whennameis truthy, and iterates when it is a list. A bare{{else if}}/{{else unless}}with no condition is now a compile error. To restore the previous bare-name behavior, where the name was ignored and the branch rendered as a plain{{else}}, set the newCompatibility.LegacyBareElseNameBehavior = true. {{else if a b}}/{{else unless a b}}(too many arguments) and{{else}}followed by another{{else}}now throwHandlebarsCompilerException. Before, these rendered the branch or threwArgumentNullException.- Chains that used to throw
ArgumentNullExceptionnow work:{{else if b includeZero=true}}, and a custom-helper chain followed by{{else}}.
Template Data Before 2.5.0 {{#if user.isAdmin}}Admin tools{{else user.isModerator}}Moderator tools{{/if}}neither admin nor moderator Moderator tools(empty) {{#if a}}A{{else unless b}}B{{else}}C{{/if}}a=false, b=false CB{{#if a}}A{{else ifEq b "y"}}B{{/if}}(ifEqregistered)a=false, b="x" 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 BHandlebarsCompilerException -
JsonNodebehavior (#670 by @jsneedles):false,0and""in aJsonNodeare now falsy in#ifand similar helpers. Before, they were truthy.JsonObjectmember lookups now fall back to the lowercase name, asJsonElementlookups do.- Custom member alias providers are now consulted for unresolved
JsonElementandJsonObjectmembers.
-
Compile error messages and exception types:
{{#if}}/{{#unless}}with no argument now throwsHandlebarsCompilerException(#if requires exactly one argument). Before, it threwInvalidOperationException. With too many arguments, it throws the same exception type as before, but the message now reads the same way. (#678, fixes #674)- When a compile error has a known position, its message now ends with
Line n, column n.followed by the template line and a caret under the error. The oldOccured at: n:ntext is gone. Code that compares the full message exactly, or searches it forOccured at:, needs updating. Use the newHandlebarsException.Line/Columnproperties instead. (#680, #681, #682, fixes #314)
New features
JsonNodesupport.JsonObject,JsonArrayandJsonValuecan be passed directly as template data. They support member access,#eachwith@index/@key/@first/@last, truthiness,includeZeroand partials, the same asJsonElement.JsonElementarrays now support index access ({{items.[0]}},{{lookup items 1}}) and{{items.Count}}, plus{{items.length}}withUseCollectionMemberAliasProvider(). One difference to note: when output directly, booleans render asTrue/Falsefrom aJsonElementand astrue/falsefrom aJsonNode. New public types:JsonNodeObjectDescriptorProvider,JsonNodeMemberAccessor,JsonNodeIterator. There are also newJsonElementObjectDescriptorProvider/JsonElementMemberAccessorconstructor overloads that take alias providers. (#670 by @jsneedles)- Error locations on exceptions. The new
HandlebarsException.LineandHandlebarsException.Columnproperties (zero-based,int?) are inherited byHandlebarsCompilerException,HandlebarsParserExceptionandHandlebarsRuntimeException. Compile errors now show the offending source line with a caret:Unclosed blocks, stray closing tags and mismatchedUnexpected '!File' inside an expression: '!' can only start a comment (e.g. '{{! comment }}') and is not a negation operator. Use '#unless' to negate a condition Line 0, column 9. <p>{{#if !File}}yes{{/if}}</p> ^}}/}}}now report where they occurred.{{#if !x}}used to fail with an internal "must be reducible node" error and now gets the clear message shown above. (#680, #681, #682, fixes #314) - New compatibility flags:
Compatibility.LegacyStandaloneCaretBehavior,Compatibility.BlockScopedInlinePartialsandCompatibility.LegacyBareElseNameBehavior, as described above. They are documented in the README under "Compatibility feature toggles".
Fixes
{{#each}}over items of different runtime types now looks up each item's members by that item's own type. Before, a dictionary followed by a number threwInvalidCastException, and a number followed by a dictionary rendered empty. (#691, fixes #685; based on #686 by @bardrop)- Raw block helpers (
{{{{raw}}}}…{{{{/raw}}}}) compile again whenCompatibility.RelaxedHelperNaming = true. (#677, fixes #676)
Internal
- Refactored the tokenizer,
LiteralParserandReusableStringWriterto address SonarCloud findings, with no behavior change. (#679, refs #641)
Contributors: @jsneedles, @EnesKartal, @bardrop, @rexm
Full Changelog: 2.4.3...2.5.0