Skip to content

Instantly share code, notes, and snippets.

@greggnakamura
Last active November 16, 2015 04:38
Show Gist options
  • Save greggnakamura/776305837443493cdb87 to your computer and use it in GitHub Desktop.
Save greggnakamura/776305837443493cdb87 to your computer and use it in GitHub Desktop.
Javascript: Handlebars JS basic template example
<script id="template" type="text/x-handlebars-template">
<p>Use the <strong>{{power}}</strong>, {{name}}!</p>
</script>
{!-- http://www.sitepoint.com/overview-javascript-templating-engines/ --}
{!-- output: Use the force, Luke! --}
//Grab the inline template
var template = document.getElementById('template').innerHTML;
//Compile the template
var compiled_template = Handlebars.compile(template);
//Render the data into the template
var rendered = compiled_template({name: "Luke", power: "force"});
//Overwrite the contents of #target with the renderer HTML
document.getElementById('target').innerHTML = rendered;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment