Skip to content

Instantly share code, notes, and snippets.

@dimkoug
Last active June 15, 2022 14:32
Show Gist options
  • Save dimkoug/be06dd18b480e3f632c21ec64dce3180 to your computer and use it in GitHub Desktop.
Save dimkoug/be06dd18b480e3f632c21ec64dce3180 to your computer and use it in GitHub Desktop.
Handlebars example
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.js"></script>
<script type="text/javascript">
'use strict'
$(document).ready(function(){
var items = [
{
name: "test",
categories: [
{id:1,name:"category1"},
{id:2,name:"category2"},
]
},
{
name: "test2",
categories: [
{id:3,name:"category3"},
{id:4,name:"category4"},
]
},
]
var template2 = $('#handlebars-demo2').html();
var templateScript2 = Handlebars.compile(template2);
var context2 = { "items" : items };
var html2 = templateScript2(context2);
$(document.body).append(html2);
})
</script>
</head>
<body>
<script id="handlebars-demo2" type="text/x-handlebars-template">
<ul>
{{#each items}}
<li>{{this.name}}
<ul>
{{#each this.categories}}
<li> {{this.id}} {{this.name}}</li>
{{/each}}
</ul>
</li>
{{/each}}
</ul>
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment