Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@guysmoilov
Created March 11, 2015 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guysmoilov/de11014dab0c1a09dc68 to your computer and use it in GitHub Desktop.
Save guysmoilov/de11014dab0c1a09dc68 to your computer and use it in GitHub Desktop.
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