Skip to content

Instantly share code, notes, and snippets.

@oleics
Created October 18, 2016 16:12
Show Gist options
  • Save oleics/d28c0beee49ed6f8f6248d2e20b41dcc to your computer and use it in GitHub Desktop.
Save oleics/d28c0beee49ed6f8f6248d2e20b41dcc to your computer and use it in GitHub Desktop.
nunjucks filter
nunjucksEnv.addFilter('debug', function(obj){
return '<pre>'+jsonStringifySafe(obj, ' ')+'</pre>';
});
function jsonStringifySafe(d, space) {
var cache = [];
d = JSON.stringify(d, function(key, value) {
if(typeof value === 'object' && value !== null) {
if(cache.indexOf(value) !== -1) {
return '[*circular]';
}
cache.push(value);
}
return value;
}, space);
cache = null;
return d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment