我有如下数组
var data = [
{
title: 'This is title',
desc: 'This is desc',
date: '07:12'
},
{
title: 'This is title2',
desc: 'This is desc2',
date: '04:12'
},
{
title: 'This is title3',
desc: 'This is desc3',
date: '09:12'
}
];
现在我想遍历这些数据以使用 underscorejs 模板显示。我正在尝试以下哪个不起作用。
<% _.each(function () { %>
<li>
<span class="time"><%= date %></span>
<p><%= title %></p>
<p><%= desc %></p>
</li>
<% }); %>
上面的代码没有显示任何内容,也没有在控制台中显示任何错误。如何遍历这个数组数据以显示所有数据?
更新
这是更多代码。我正在从主干视图传递这些数据
var Message = Backbone.View.extend({
className: 'tops',
render: function () {
console.log(this.model.toJSON()); //<-- see output for this below
this.$el.html(_.template(MessageTemplate, this.model.toJSON()));
return this;
}
});
console.log() 输出
Object {title: "This is title", desc: "This is desc", date: "07:12"} message.js:6
Object {title: "This is title2", desc: "This is desc2", date: "04:12"} message.js:6
Object {title: "This is title3", desc: "This is desc3", date: "09:12"}
我将上面的对象传递给我的模板,然后循环显示它。