Last active
December 20, 2015 12:39
-
-
Save frankV/6133166 to your computer and use it in GitHub Desktop.
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
<script> | |
document.addEventListener("DOMContentLoaded", function() { | |
//Get the contents | |
var source = document.querySelector("#results-template").innerHTML; | |
//Compile them into a template | |
template = Handlebars.compile(source); | |
// add the formatName helper | |
Handlebars.registerHelper("formatName", function(fname, lname) { | |
return fname + " " + lname.substr(0,1)+'.'; | |
}); | |
// add the userName helper | |
Handlebars.registerHelper("userName", function(fname, lname) { | |
return fname.substr(0,1).toLowerCase() + lname.toLowerCase(); | |
}); | |
// on click of #demobutton, populate values and display template | |
document.querySelector("#demoButton").addEventListener("click", | |
function() { | |
var fname = document.querySelector("#firstname").value; | |
var lname = document.querySelector("#lastname").value; | |
var things = document.querySelector("#things").value; | |
if(things.length) var arrThings = things.split(","); | |
// html is an array of "things" objects | |
var html = template({firstname:fname, lastname:lname, things:arrThings}); | |
document.querySelector("#resultDiv").innerHTML = html; | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment