Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Meteor template that can recursively display any document's structure.
<template name="fieldSpreader">
{{#if isArray}}
{{> arraySpreader}}
{{else}}
{{#if isObject}}
{{> objectSpreader}}
{{else}}
{{!-- This is just a value type --}}
{{ this}}
{{/if}}
{{/if}}
</template>
<template name="objectSpreader">
<ul>
{{#each keyValuePair}}
<li>{{key}}:{{> fieldSpreader value}}</li>
{{/each}}
</ul>
</template>
<template name="arraySpreader">
<ul>
{{#each this}}
<li>{{> fieldSpreader}}</li>
{{/each}}
</ul>
</template>
{!-- This is the containing template you should include with a document context --}}
<template name="documentSpreader">
{{> objectSpreader}}
</template>
spreaderIsArray = function(obj) {
return (obj && obj.constructor && (obj.constructor === Array));
}
spreaderIsObject = function(obj) {
return ((!spreaderIsArray(obj)) && obj && obj.constructor && (obj.constructor === Object));
}
Template.fieldSpreader.helpers({
isObject: function() {
return spreaderIsObject(this);
},
isArray: function() {
return spreaderIsArray(this);
}
});
Template.objectSpreader.helpers({
keyValuePair: function() {
var pairs = [];
for (k in this) {
pairs.push({
key: k,
value: this[k]
});
}
return pairs;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment