Created
February 10, 2014 22:50
-
-
Save curiouslychase/8925848 to your computer and use it in GitHub Desktop.
A snippets helper for debugging handlebars. Appends debug to all templates so you don't have to type it in yourself, and when a template is rendered, the context is logged in the console.
This file contains 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
var scriptsNodeList = document.getElementsByTagName('script'), | |
debugSet = false, | |
templates, scripts; | |
// convert node list to array | |
scripts = Array.prototype.slice.call(scriptsNodeList); | |
templates = scripts.filter(function(script) { | |
return script.type === 'text/x-handlebars-template'; | |
}).forEach(function(template) { | |
if (debugSet) return; | |
var debug = document.createTextNode('{{debug}}'); | |
template.appendChild(debug); | |
}); | |
debugSet = true; | |
/** | |
Use: In templates add {{debug}} | |
*/ | |
Handlebars.registerHelper("debug", function(optionalValue) { | |
console.log("Current Context"); | |
console.log("===================="); | |
for(var key in this) { | |
console.log('%c' + key + ': ', 'background-color: #00CDAC; color: #fff; padding: 3px;') | |
console.log(this[key]); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment