Skip to content

Instantly share code, notes, and snippets.

@BFalkner
Created February 9, 2012 14:42
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 BFalkner/1780430 to your computer and use it in GitHub Desktop.
Save BFalkner/1780430 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Template Test</title>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0.beta2/handlebars.min.js"></script>
<script type="text/javascript">
$(function() {
$("script[type='text/x-handlebars-template']").each(function(i, el) {
var source = $(el).data("source");
var template = $(el).data("template");
$.when(source ? $.getJSON(source) : $.Deferred().resolve([{}]),
template ? $.get(template) : $.Deferred().resolve([$(el).html()]))
.then(function(json, handlebars) {
var compiled = Handlebars.compile(handlebars[0]);
$(el).replaceWith(compiled(json[0]));
});
});
});
</script>
</head>
<body>
<script data-source="/contacts" type="text/x-handlebars-template">
<ul>
{{#each contacts}}
<li>{{firstname}} {{lastname}}</li>
{{/each}}
</ul>
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment