admin管理员组文章数量:1431398
here is the 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"
}
}
];
and my mustache template is:
{{#event}}
<div>
<h2>{{name}}</h2>
<span>on {{data}}</span>
<p>{{address}}</p>
</div>
{{/event}
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 the 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"
}
}
];
and my mustache template is:
{{#event}}
<div>
<h2>{{name}}</h2>
<span>on {{data}}</span>
<p>{{address}}</p>
</div>
{{/event}
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?
Share Improve this question asked Jul 23, 2011 at 7:44 eddie yangeddie yang 1,2912 gold badges13 silver badges15 bronze badges1 Answer
Reset to default 4here 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}}
Hope that helps
本文标签: javascripthow can I render this JSON use mustachejs without loopStack Overflow
版权声明:本文标题:javascript - how can I render this JSON use mustache.js without loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745509737a2661411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论