我正在使用Mustache并使用数据
{ "names": [ {"name":"John"}, {"name":"Mary"} ] }
我的 mustache 模板是:
{{#names}}
{{name}}
{{/names}}
我想要做的是获取数组中当前数字的索引。像这样的东西:
{{#names}}
{{name}} is {{index}}
{{/names}}
并打印出来
John is 1
Mary is 2
可以用 Mustache 得到这个吗?或者使用 Handlebars 或其他扩展?
最佳答案
这就是我在 JavaScript 中的做法:
var idx = 0;
var data = {
"names": [
{"name":"John"},
{"name":"Mary"}
],
"idx": function() {
return idx++;
}
};
var html = Mustache.render(template, data);
您的模板:
{{#names}}
{{name}} is {{idx}}
{{/names}}
关于templates - 在 Mustache 中,如何获取当前部分的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5021495/