Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Handlebars template
<!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