Skip to content

Instantly share code, notes, and snippets.

@milankorsos
Created November 1, 2011 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milankorsos/1329750 to your computer and use it in GitHub Desktop.
Save milankorsos/1329750 to your computer and use it in GitHub Desktop.
underscore templates
<script type="text/template" id="something-tmpl">
<% for (var index = 0; index < items.length; index++) { %>
<% var item = items[index]; %>
<p><strong><%= item.username %></strong></p>
<% if (item.age) { %> <p>Age: <%= item.age %></p> <% } %>
<% if (item.sex) { %> <p>Sex: <%= item.sex %></p> <% } %>
<% if (item.location) { %> <p>Location: <%= item.location %></p> <% } %>
<% } %>
</script>
// initialization()
template = _.template($("#something-tmpl").html());
// render()
var items = {
id: 1,
username: milan,
age: 25,
sex: "Guy",
location: "San Francisco"
};
$(element).html(template({items : items }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment