How to pass additional variables in to underscores templates

女生的网名这么多〃 提交于 2019-12-01 19:10:25
Mike

I'm not sure that I fully understand your question. But I do something along these lines to pass multiple objects into my template function.

 $(this.el).html(_.template(html)($.extend({}, this.model.toJSON(), App.lists.toJSON())))

The jquery extend function lets me merge two objects into one. So in your case you could merge your "searchTerm" in with your model during templating.

_.template(html)($.extend({}, row.doc, {"searchTerm": searchTerm}))

Your other option would be to pass your entire model into you template function and perform your underscore iteration in the template. Let me know if you need more explanation on that.

EDIT:

Here is a link to jquery extend

And underscore also has and extend method if you aren't using jquery

EDIT EDIT:

This is just to give you an example of my second suggestion. Would probably need some tweaking to throw into yours, but this is the idea.

template: _.template("

    <% _(rows).each(function(row) { %>
        <tr><td><% print(someKey.replace(searchTerm, '<b>' + searchTerm + '</b>')); %></td></tr>
<% } %>

"),
render: function(){
    this.el.append(this.template(this.model.toJSON()));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!