Skip to content

Instantly share code, notes, and snippets.

@jshaw
Created August 26, 2014 21:26
Show Gist options
  • Save jshaw/968d6e5408e32ef8e3f5 to your computer and use it in GitHub Desktop.
Save jshaw/968d6e5408e32ef8e3f5 to your computer and use it in GitHub Desktop.
example javascript templates using underscore.js templating
<!--Example JS template-->
<script type="text/template" id="PastCommunityTemplate">
<img class="pvs-cmy-img" src="<%= thumbnail %>" alt="<%= name %>">
<h3 class="pvs-cmy-title"><%= name %></h3>
<h4 class="pvs-cmy-city">City: <%= city %></h4>
<p class="pvs-cmy-description"><%= summary %></p>
</script>
// Test Object for template
this.object = {
thumbnail: 'asfasdfasdf.jpg',
name: 'jordan',
city: 'toronto',
summary: 'this is an example'
};
// Gets what is inside of the JS template and treats is as html
// stores the js template function as this.tmpl
// replaces the template placeholders with what is passed in the object
this.tmpl = _.template($('#PastCommunityTemplate').html();
this.$element.append(this.tmpl(this.object));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment