I'm working on some CouchDB apps and it's been lots of fun. I'm also learning how to use Mustache.js for templating. My question is what is the best approach for templating when your data has many nested layers. Here's an example:
var jsondata = {
post1: {
title: "First blog post",
author: "John",
tags: ["awesome", "interesting", "philosophical"],
comments: {
julie: "I love it!",
mark: "I hate it!"
}
},
post2: {
title: "Second blog post",
author: "Jill",
tags: ["extraordinary", "crazy", "nice"],
comments: {
julie: "I love it!",
mark: "I hate it!"
}
};
I realize it depends on the unique structure of the data, but is there a general approach that works well? Here are some I'm considering:
- Extract nested data from the set, so that you have a collection of top level arrays, then use helper functions to compile those into a template.
- Build the template to mirror the data, then use some crazy combination of $.each, templating and regular expressions to compile the template in one shot.
- Break the compilation down into parts, and then pass through the template several times, until it's fully complete.
I'm pretty lost here, would love some help!
PS: I'm planning on separating blog posts, comments, and tags, into separate json docs, and then pulling them together with couchdb views and lists, so I'm still a bit uncertain on the resulting data structure.