过滤传递给下划线模板的数据

filter data passed into an underscore template

我的页面有 5 个不同的标签。

我有一个包含多个对象的 templateData 对象。

我想将我的 templateData 对象中符合特定条件的对象列表传递到每个选项卡。

例如

$("#cat-37").append(template(_.where(templateData.listItems, {cat_group: "37"})));

我的模板数据对象中的示例对象

    {cat_group:"37",
skill_id:"56",
skill_title:"Neutral/Stable Position",
status:"open",
tunnel:"Denver"}

我可以让 _.where() 函数在外部工作,但是 template() 函数要求的格式有一些问题被绊倒了。有什么想法吗?

我认为您必须将数据作为对象传递。

//list of objects
var data = _.where(templateData.listItems, {cat_group: "37"});
$("#cat-37").append(template({data:data}));

然后在你的模板中

<script type="text/template" id="myTemplate">
<% _.each(data,function(d){ %>
    //iterate thru data and do your thing
<% }) %>
</script>