I have an object with has multiple similar complex properties. I would like to write a generic template that could be used for each property but am unsure how to proceed. My object looks similar to the below json representation. if the appointments / days were a collection, then I could iterate the collection binding a template normally. However, I need to model each ‘day’ as a property – for example, there will always be 5 entries.
However, I suppose the question is can you reuse the same template for each day. In effect, iterating each property using reflection?
{
"days" : {
"monday" : {
"salesman" : "john",
"customer" : "fred"
},
"friday" : {
"salesman" : "paul",
"customer" : "keith"
},
"tuesday" : {
"salesman" : "dave",
"customer" : "graham"
},
"wednesday" : {
"salesman" : "james",
"customer" : "keith"
},
"thursday" : {
"salesman" : "keith",
"customer" : "donald"
}
}
}
If you can change the format to something like this that would make it a lot easier because you can just use an iterator over days, and interpolate using known keys.
However, using the existing format you can solve this in Handlebars.js like this (should probably work in Mustache.js as well, but I haven’t tried that):