I’m trying to filter out elements in an Underscore template. For example I want only to return the elements that do not have a class name of “pin”. Anyone know how I could do that either with underscore methods or jquery
var renderToHTML = function( data ) {
var list = data.results[0].items;
var tmpl = $( template({items : list}) );
// var layout = tmpl.filter()
}
My template code looks like this.
<% _.each(items,function(item,i){ %>
<% var cls=( item.id == 'feature') ? "pin" : 'item'; %>
<div class="<%=cls%> <%=item.id%>" style="width:<%=item.width%>px">
<div class="itemInner">
<img src="<%= item.img %>" />
<div class="title"><%= item.title %></div>
</div>
</div>
<% }); %>
If you want to extract the
<div>s without a class ofpinfrom the generated HTML, then you’d probably want to use:not:Or, given the logic inside your template, you might be able to look for
.iteminstead:If you only want to include non-
pins in the HTML that comes out of your template, then:Or perhaps use
_.chainand_.filter:If you want to do it at this level:
Then you’d want to use jQuery’s
filterinstead offind:or, again, given the specific structure of your template: