I have a JSON structure similar as below.
"markers": {
"Lifestyle" : [
{"Name1" : "Something"},
{"Name2" : "Something"}
],
"Transport" : [
{"Name1" : "Something"},
{"Name2" : "Something"}
]
I want to create a menu with nested list-item using the above JSON.
<ul id="menu">
<li class="Lifestyle">Lifestyle
<ul class="sub-cat">
<li>Something</li>
<li>Something</li>
</ul>
</li>
<li class="Transport">Transport
<ul class="sub-cat">
<li>Something</li>
<li>Something</li>
</ul>
</li>
</ul>
I have tried adding a helper function to extract the name of the properties but i’m not sure how to add the sub-categories. Any advise is appreciated.
Edit: Added code
Handlebars.registerHelper('getCategory',function(object) {
var categories = [];
for(var x in object) {
categories.push(x);
}
console.log(categories);
return categories;
});
This just returns the category names. I know i can return HTML using the same helper but i’m not sure how i would add the nested structure.
In your case, the helper would be an iterator1 converting an object to a list of (key, value) pairs. NOte that an helper has to return HTML:
And your template could look like this
The nested level has to go through an each helper before being converted to a pair since the underlying data in an array.
Finally, a Fiddle http://jsfiddle.net/hnrQC/ to see this code in action.
1 See Block helpers in Handlebars: simple iterators