Handlebars.js: 0 is true

Created on 27 Aug 2013  路  6Comments  路  Source: handlebars-lang/handlebars.js

The built-in IF helper treats "0" (zero) as true. I understand that it would be one extra check, but I think you should be able to use it as a false value.

blocker bug

Most helpful comment

Not sure if this is too late but, this should be added to Handlebras documentation.

All 6 comments

I was bit by this, too. While it's JS's standard view of truthiness, I think the templating engine should have a different understanding of it, to make the templates simpler.

There are a few different requests for how if behaves. Given that the falsy behavior is the behavior in the 1.0.0 release we should maintain that functionality moving forward and create a flag for those who may desire numeric or other sorts of behaviors.

I agree that creating a flag is the way to go. There's definitely enough justification for allowing to treat zero as truthy. Could the flag be implemented on an element level, e.g. <#if degree zero="true">{{degree}} ?

@artiee the next release will have a includeZero flag. This can be used like so:

{{#if goodbye includeZero=true}}GOODBYE {{/if}}cruel {{world}}!

Released in 1.1.0

Not sure if this is too late but, this should be added to Handlebras documentation.

Was this page helpful?
0 / 5 - 0 ratings