如何不使用循环而使用mustache.js呈现此JSON [英] how can I render this JSON use mustache.js without loop
本文介绍了如何不使用循环而使用mustache.js呈现此JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是JSON:
var data = [
{
"event": {
"name": "txt1",
"data": "2011-01-02",
"address": "Guangzhou Tianhe Mall"
}
},
{
"event": {
"name": "txt2",
"data": "2011-01-02",
"address": "Guangzhou Tianhe Mall"
}
},
{
"event": {
"name": "txt3",
"data": "2011-01-02",
"address": "Guangzhou Tianhe Mall"
}
}
];
我的胡子模板是:
{{#event}}
<div>
<h2>{{name}}</h2>
<span>on {{data}}</span>
<p>{{address}}</p>
</div>
{{/event}
所以上面的模板代码不起作用.我现在要做的是创建一个for
循环:
so the template code above don not work.What I do now is make a for
loop :
var html = "";
for(var i = 0; i < data.length; i++){
html += Mustache.to_html(tmp, data[i]);
}
有没有更好的方法可以使其无循环运行?
Any better way to make it works without any loop?
推荐答案
这是仅使用胡须模板即可完成此操作的一种方法.您将数据设置如下:
here is one way to do the same with just mustaches templates. you set your data as follows:
var data = {data: [
{
"event": {
"name": "txt1",
"data": "2011-01-02",
"address": "Guangzhou Tianhe Mall"
}
},
{
"event": {
"name": "txt2",
"data": "2011-01-02",
"address": "Guangzhou Tianhe Mall"
}
},
{
"event": {
"name": "txt3",
"data": "2011-01-02",
"address": "Guangzhou Tianhe Mall"
}
}
]};
,您的模板应如下所示:
and your template should look as follows:
{{data}}
{{#event}}
<div>
<h2>{{name}}</h2>
<span>on {{data}}</span>
<p>{{address}}</p>
</div>
{{/event}
{{/data}}
希望有帮助
这篇关于如何不使用循环而使用mustache.js呈现此JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文