Skip to content

2.5.0

Latest

Choose a tag to compare

@stencil-harness stencil-harness released this 22 Nov 22:52
· 2 commits to master since this release
92a86d0

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, set Compatibility.LegacyStandaloneCaretBehavior = true. (#675, fixes #672; #692)

    Template Data Before 2.5.0
    {{#if condition}}yes{{^}}no{{/if}} condition=true yes{ condition = True }no yes
    {{#if condition}}yes{{^}}no{{/if}} condition=false (empty) no
    {{#each people}}{{name}}{{^}}No one{{/each}} [] (empty) No one

    A 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.BlockScopedInlinePartials setting defaults to true. To restore the previous behavior, where such partials leaked into the enclosing scope and replaced outer partials with the same name, set it to false. 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 inner inner outer
    {{#with .}}{{#*inline "myPartial"}}success{{/inline}}{{/with}}{{> myPartial}} success throws: partial could not be resolved
    {{> macros}}{{> button label="OK"}}, where partial macros declares {{#*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 has unless semantics. Before, it was evaluated as {{else if x}}, which inverted the condition.
    • {{else someHelper x}} now invokes someHelper. Before, it ignored the helper and used x as the condition. If the helper isn't registered, rendering throws the usual missing-helper HandlebarsRuntimeException.
    • A bare {{else name}} now opens a block on name. If name is a registered block helper it is invoked. Otherwise the branch renders only when name is 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 new Compatibility.LegacyBareElseNameBehavior = true.
    • {{else if a b}} / {{else unless a b}} (too many arguments) and {{else}} followed by another {{else}} now throw HandlebarsCompilerException. Before, these rendered the branch or threw ArgumentNullException.
    • Chains that used to throw ArgumentNullException now 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 C B
    {{#if a}}A{{else ifEq b "y"}}B{{/if}} (ifEq registered) 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 B HandlebarsCompilerException
  • JsonNode behavior (#670 by @jsneedles):

    • false, 0 and "" in a JsonNode are now falsy in #if and similar helpers. Before, they were truthy.
    • JsonObject member lookups now fall back to the lowercase name, as JsonElement lookups do.
    • Custom member alias providers are now consulted for unresolved JsonElement and JsonObject members.
  • Compile error messages and exception types:

    • {{#if}} / {{#unless}} with no argument now throws HandlebarsCompilerException (#if requires exactly one argument). Before, it threw InvalidOperationException. 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 old Occured at: n:n text is gone. Code that compares the full message exactly, or searches it for Occured at:, needs updating. Use the new HandlebarsException.Line / Column properties instead. (#680, #681, #682, fixes #314)

New features

  • JsonNode support. JsonObject, JsonArray and JsonValue can be passed directly as template data. They support member access, #each with @index / @key / @first / @last, truthiness, includeZero and partials, the same as JsonElement. JsonElement arrays now support index access ({{items.[0]}}, {{lookup items 1}}) and {{items.Count}}, plus {{items.length}} with UseCollectionMemberAliasProvider(). One difference to note: when output directly, booleans render as True/False from a JsonElement and as true/false from a JsonNode. New public types: JsonNodeObjectDescriptorProvider, JsonNodeMemberAccessor, JsonNodeIterator. There are also new JsonElementObjectDescriptorProvider / JsonElementMemberAccessor constructor overloads that take alias providers. (#670 by @jsneedles)
  • Error locations on exceptions. The new HandlebarsException.Line and HandlebarsException.Column properties (zero-based, int?) are inherited by HandlebarsCompilerException, HandlebarsParserException and HandlebarsRuntimeException. Compile errors now show the offending source line with a caret:
    Unexpected '!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>
             ^
    
    Unclosed blocks, stray closing tags and mismatched }} / }}} 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.BlockScopedInlinePartials and Compatibility.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 threw InvalidCastException, and a number followed by a dictionary rendered empty. (#691, fixes #685; based on #686 by @bardrop)
  • Raw block helpers ({{{{raw}}}}…{{{{/raw}}}}) compile again when Compatibility.RelaxedHelperNaming = true. (#677, fixes #676)

Internal

  • Refactored the tokenizer, LiteralParser and ReusableStringWriter to address SonarCloud findings, with no behavior change. (#679, refs #641)

Contributors: @jsneedles, @EnesKartal, @bardrop, @rexm

Full Changelog: 2.4.3...2.5.0