Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.
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