我是JSON和小胡子的新手。我正在尝试迭代我使用Mustache创建的数组,并且我遇到了一些问题。我的代码如下所示:
var shows=[
{"title":"Strawberry Shortcake","description":"A show about a cake","video":"none","category":"chilren"},
{"title":"Vanilla Ice","description":"A show about a ice","video":"none","category":"adult"}
];
var template="{{#shows}}{{.}}{{/shows}}";
var html=Mustache.render(template,shows);
document.write(html);
答案 0 :(得分:2)
你想要"表演"为了正确迭代,在哈希中:
var shows={"shows":[
{"title":"Strawberry Shortcake","description":"A show about a cake","video":"none","category":"chilren"},
{"title":"Vanilla Ice","description":"A show about a ice","video":"none","category":"adult"}
]};
var template="{{#shows}}{{.}}{{/shows}}";
var html=Mustache.render(template,shows);
document.write(html);
这将产生多次生成模板的预期效果。
<强>更新强>
关于Lambdas的问题。我只是在manual中查看了这个内容。我认为它涵盖了你所询问的内容:
当值是可调用对象时,例如函数或lambda, 将调用对象并传递文本块。文字通过了 是文字块,没有渲染。 {{tags}}不会被扩展 - lambda应该自己做。通过这种方式,您可以实现过滤器或缓存。
模板:
{{#wrapped}}
{{name}} is awesome.
{{/wrapped}}
哈希:
{
"name": "Willy",
"wrapped": function() {
return function(text) {
return "<b>" + render(text) + "</b>"
}
}
}
答案 1 :(得分:1)
var shows=[ {"title":"Strawberry Shortcake","description":"A show about a cake","video":"none","category":"chilren"}, {"title":"Vanilla Ice","description":"A show about a ice","video":"none","category":"adult"} ]; var template="{{#.}}title:{{title}},video:{{video}}{{/.}}"; var html=Mustache.render(template,shows); document.write(html);