Display value "0" using mustache pattern

I have a model with an attribute with a value of "0" (zero). My template looks something like this:

{{#count}}{{{count}}} items{{/count}} {{^count}}-{{/count}} 

If myModel.count = 0 , the displayed html is nothing. It as the value of "count" is null and not equal to zero.

Mustache documentation for this case: https://github.com/janl/mustache.js#inverted-sections

+4
source share
2 answers

@bobthecow answered this in his comment:

It does not display zero because zero is false. One of the options to get it for display should be set to non-false (for example, the string "0")

+1
source

One method is to use lambda to check the value.

http://jsfiddle.net/Bodman/yb83s/

 var data = { "dataset" : [ {count: 0}, {count: -1}, {count: 2}, {count: false}, {no_count:'yay'} ], "check_zero": function () { return function (text, render) { var result = ''; var count = this.count; if(!isNaN(parseFloat(count)) && isFinite(count)){ result = count; }else { result = render(text); } return result; } } } 

Hope this helps.

0
source

Source: https://habr.com/ru/post/1446296/


All Articles