Created
May 23, 2017 23:01
-
-
Save delitescere/f64e5150a0820fa5f152d5621aaae45c to your computer and use it in GitHub Desktop.
Handlebars client-side templates
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> | |
<head> | |
<title>Greeting</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.10/handlebars.min.js"></script> | |
</head> | |
<body> | |
<h1>Greetings!</h1> | |
<script type="text/handlebars"> | |
{{#people}} | |
<p>Hello, {{name}}!</p> | |
{{/people}} | |
</script> | |
</body> | |
<script> | |
'use strict'; | |
var model = { | |
"people": | |
[ | |
{ | |
id: "one", | |
name: "Josh" | |
}, | |
{ | |
id: "two", | |
name: "Nick" | |
} | |
] | |
}; | |
var scripts = Array.prototype.slice.call(document.getElementsByTagName("script")); | |
var templates = scripts.filter(script => script.type == "text/handlebars"); | |
templates.forEach(template => { | |
var output = Handlebars.compile(template.innerHTML); | |
template.outerHTML = output(model); | |
}); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment