小胡子用一系列对象模板化 [英] Mustache templating with an array of objects
本文介绍了小胡子用一系列对象模板化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试模拟以下对象数组:
I am trying to template the following array of objects:
var arr = [{name:"Ryan Pays", url:"http://www.ryanpays.com"}, {name:"foo", url:"http://www.google.com"}];
我将该数组转换为如下对象:
I convert that array to an object like so:
arr = $.extend({}, arr);
这给了我以下对象:
{
0:{name:"Ryan Pays", url:"http://www.ryanpays.com"},
1:{name:"foo", url:"http://www.google.com"}
}
使用Mustache我想使用以下模板枚举该对象:
Using Mustache i want to enumerate over that object with the following template:
var template = "<h4>Your friends' choices</h4>" +
"<ul>" +
"<li>" +
"<p><strong>{{name}}</strong> likes <a href='{{url}}'>this</a></p>" +
"</li>" +
"</ul>";
var html = Mustache.to_html(template, displayData);
$('.choices').html(html);
我似乎无法做到这一点。我可以得到第一个结果像是这样打印:
I don't seem to be able to do that. I can get the first result like to print like so:
var html = Mustache.to_html(template, displayData[0]);
等等但不是两者兼而有。
And so on but not both.
链接到这个问题的小提琴:
Link to fiddle of this issue:
http:// jsfiddle .net / AtJDa /
推荐答案
您可以将模板用于数组:
You could use the template for arrays :
var template = "<h4>Your friends' choices</h4>" +
"<ul>" +
"{{#arr}}"+
"<li>" +
"<p><strong>{{name}}</strong> likes <a href='{{url}}'>this</a></p>" +
"</li>" +
"{{/arr}}"+
"</ul>";
var html = Mustache.to_html(template, {arr:arr});
这篇关于小胡子用一系列对象模板化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文