Last active
June 12, 2017 08:11
-
-
Save pste/92901a0bd921500e073f5244393ecd18 to your computer and use it in GitHub Desktop.
Handlebars template
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> | |
<meta charset="UTF-8"> | |
<head> | |
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.10/handlebars.min.js" crossorigin="anonymous"></script> | |
</head> | |
<body> | |
<div id="main"> | |
<script type="text/x-handlebars-template"> | |
<table> | |
<thead> | |
<tr> | |
<th>Name</th> | |
<th>Age</th> | |
</tr> | |
</thead> | |
<tbody> | |
{{#each .}} | |
<tr> | |
<td>{{name}}</td> | |
<td>{{age}}</td> | |
</tr> | |
{{/each}} | |
</tbody> | |
</table> | |
</script> | |
</div> | |
<script> | |
var items = [ | |
{name: "enzo", age:52}, | |
{name: "beppe", age: 33}, | |
{name: "ste", age: 41} | |
]; | |
(function(data) { | |
var source = $('#main script').text(); // no html to avoid inner rendering | |
var template = Handlebars.compile(source.trim()); | |
$('#main').html(template(data)); | |
}(items)) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment