Skip to content

Instantly share code, notes, and snippets.

@zmcartor
Created December 5, 2011 01:46
Show Gist options
  • Save zmcartor/1431957 to your computer and use it in GitHub Desktop.
Save zmcartor/1431957 to your computer and use it in GitHub Desktop.
underscorejs templating
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.2.2/underscore-min.js" type="text/javascript"></script>
<script type="text/javascript">
album1 = {artist:"Sonic Youth" , album:'Dirty'}
album2 = {artist:"Cher" , album: 'blah'}
album3 = {artist:"Silverchair", album:'Frog Stomp'}
albums = [album1 , album2, album3]
$(function(){
html_output = _.template($('#albums-template').html() , albums)
$('#albums').append(html_output)
})
</script>
<script type="text/template" id="albums-template">
<H1> Music Library </h1>
<ul class ="albums"></ul>
<% _.each(albums, function(album) { %>
<li><%=album.artist%> released <%= album.album %></li>
<%})%>
</script>
</head>
<body>
<div id="albums"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment