-
-
Save modster/5f5cd8ccea13285e5f3af756db22c2f8 to your computer and use it in GitHub Desktop.
Handlebars example
This file contains hidden or 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 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