Created
December 20, 2011 12:26
-
-
Save micheljansen/1501412 to your computer and use it in GitHub Desktop.
Example Underscore Templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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