浏览代码

Don't evaluate lambdas that return falsy values as an empty string.

Issue: If I create a Mustache lambda to return numbers and I expect 0 to be of
those numbers, I'll instead only receive an empty string for 0 because it
evaluates to falsy.

Fix: Check the result of the lambda to be undefined or null. Only then should
it return the empty string.
tags/0.7.1
Matt Sacks 13 年前
父节点
当前提交
d59f02f622
共有 4 个文件被更改,包括 11 次插入1 次删除
  1. +2
    -1
      mustache.js
  2. +7
    -0
      test/_files/check_falsy.js
  3. +1
    -0
      test/_files/check_falsy.mustache
  4. +1
    -0
      test/_files/check_falsy.txt

+ 2
- 1
mustache.js 查看文件

@@ -254,7 +254,8 @@ var Mustache;
return self.render(template, context);
};

return value.call(context.view, text, scopedRender) || "";
var result = value.call(context.view, text, scopedRender);
return result != null ? result : "";
default:
if (value) {
return callback(this, context);


+ 7
- 0
test/_files/check_falsy.js 查看文件

@@ -0,0 +1,7 @@
({
number: function(text, render) {
return function(text, render) {
return +render(text);
}
}
})

+ 1
- 0
test/_files/check_falsy.mustache 查看文件

@@ -0,0 +1 @@
<p>{{#number}}0{{/number}}</p>

+ 1
- 0
test/_files/check_falsy.txt 查看文件

@@ -0,0 +1 @@
<p>0</p>

正在加载...
取消
保存