让我们看看这个片段:
{{#repos}}
<b>{{name}}</b>
{{/repos}}
如何计算
repos
中的项目数?
最佳答案
一种方法是将逻辑作为 this
上的函数添加到 JSON 本身。 :
var tmpl = "{{#repos}}<b>{{name}}</b>{{/repos}}({{count}})";
var json = {
repos: [{ name: "Tom"}, { name: "Dick"}, { name: "Harry"}],
count: function() { return this.repos.length; }
};
alert(Mustache.to_html(tmpl, json));
http://jsfiddle.net/mblase75/QBzuk/
关于mustache - 计数列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8125459/