Skip to content

Fix blockHelperMissing rendering the block for an empty string - #2178

Open
theodorejb wants to merge 1 commit into
handlebars-lang:masterfrom
theodorejb:fix-gh-2177
Open

Fix blockHelperMissing rendering the block for an empty string#2178
theodorejb wants to merge 1 commit into
handlebars-lang:masterfrom
theodorejb:fix-gh-2177

Conversation

@theodorejb

Copy link
Copy Markdown
Contributor

An empty string fell through to the truthy branch, so {{#value}} unexpectedly rendered its block instead of the inverse when value was ''. Treat '' as falsy alongside null, undefined and false.

This doesn't break any existing tests, so the previous behavior seems to have been an oversight rather than intentional. Rendering a block with an empty string as context isn't useful in practice.

This is an important compatibility fix, since it is common in Mustache templates to use a block section to conditionally output a tag or attribute only if it has content. E.g:

{{#notes}}<p class="notes">{{notes}}</p>{{/notes}}
<button{{#id}} id="{{ id }}"{{/id}}>Click me</button>
{{#user}}
  {{name}}{{^name}}<i>Anonymous</i>{{/name}}
{{/user}}

Resolves #2177

An empty string fell through to the truthy branch, so `{{#value}}` unexpectedly
rendered its block instead of the inverse when `value` was `''`. Treat `''` as falsy
alongside `null`, `undefined` and `false`.

This doesn't break any existing tests, so the previous behavior seems to have
been an oversight rather than intentional. Rendering a block with an empty
string as context isn't useful in practice.

This is an important compatibility fix, since it is common in Mustache templates
to use a block section to conditionally output a tag or attribute only if it has
content. E.g. `{{#notes}}<p class="notes">{{notes}}</p>{{/notes}}`.

Resolves handlebars-lang#2177
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.

Empty string should not be truthy for block sections

1 participant