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

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
@jaylinski
jaylinski requested a lite review from Copilot September 10, 2026 16:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
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

2 participants