Skip to content

Instantly share code, notes, and snippets.

@delitescere
Created May 23, 2017 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save delitescere/f64e5150a0820fa5f152d5621aaae45c to your computer and use it in GitHub Desktop.
Save delitescere/f64e5150a0820fa5f152d5621aaae45c to your computer and use it in GitHub Desktop.
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