Skip to content

Instantly share code, notes, and snippets.

@mientjan
Created March 16, 2015 17:52
Show Gist options
  • Save mientjan/ce073078a195607f39a9 to your computer and use it in GitHub Desktop.
Save mientjan/ce073078a195607f39a9 to your computer and use it in GitHub Desktop.
Handlebar: DumpHelper
function dump(context, options) {
function type(value){
var type = '';
if( Object.prototype.toString.call( value ) === '[object Array]' ){
type = 'array';
} else if(typeof value == 'object' ) {
type = 'object';
} else {
type = 'primitive';
}
return type;
}
function loop(value, callback){
if( Object.prototype.toString.call( value ) === '[object Array]' ) {
for(var i = 0; i < value.length; i++)
{
callback(value[i], i);
}
} else if(typeof value == 'object' ) {
for(var i in value)
{
callback(value[i], i);
}
} else {
callback(value, 0);
}
}
var dump;
dump = function(context){
var self = this;
var ret = "";
ret += '<ul>';
loop(context, function(value, index){
ret += '<li>';
ret += 'index: ' + index;
ret += '<br />';
if( type(value) != 'primitive' ){
ret += dump(value);
} else {
ret += 'value:' + value;
}
data += '</li>';
});
ret += '</ul>';
return ret;
}
return dump(context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment