Skip to content

Instantly share code, notes, and snippets.

@micheljansen
Created December 20, 2011 12:26
Show Gist options
  • Save micheljansen/1501412 to your computer and use it in GitHub Desktop.
Save micheljansen/1501412 to your computer and use it in GitHub Desktop.
Example Underscore Templates
<script type="text/template" id="listing_template">
<li class="listing">
<a href="#">
<div class="image_wrapper">
<img src="<%= img_url %>" />
</div>
<h3><%= title %></h3>
<div class="metadata">
<p>
<%= summary %>
</p>
<span class="price">£<%= price %></span>
</div>
</a>
</li>
</script>
<script type="text/template" id="listings_template">
<%
var single_listing_tmpl = $("#listing_template").html()
%>
<% _(listings).each(function(listing) { %>
<% var output = _.template(single_listing_tmpl, listing) %>
<%= output %>
<% }); %>
</script>
<script>
$(function() {
var listings_template = _.template($("#listings_template").html())
var listings_html = listings_template(listings)
$("#listings").html(listings_html)
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment