Created
October 18, 2010 07:47
-
-
Save joshsusser/631887 to your computer and use it in GitHub Desktop.
I like this approach to putting mustache/handlebars templates in script tag sections. Reads much easier than assembling them from strings in JavaScript.
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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<script src="javascripts/jquery.js" type="text/javascript"></script> | |
<script src="javascripts/handlebars.js" type="text/javascript"></script> | |
<script id="main" type="text/html"> | |
<ul> | |
{{#people}} | |
<li>{{> link}}</li> | |
{{/people}} | |
</ul> | |
</script> | |
<script id="link" type="text/html"> | |
<a href="/people/{{id}}">{{name}}</a> | |
</script> | |
<script type="text/javascript"> | |
$(document).ready(function() { | |
var main = $("#main").html(); | |
var partials = { "link": $("#link").html() }; | |
var template = Handlebars.compile(main); | |
var data = { "people": [ | |
{ "name": "Alan", "id": 1 }, | |
{ "name": "Yehuda", "id": 2 } | |
]}; | |
$("body").html( template(data, { "partials": partials }) ); | |
}); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment