Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Handlebars client-side templates
<!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
You can’t perform that action at this time.