I saw how this was discussed in several threads, and it looks like it was (or is) a way to do this. But I can't get this to work. So something is missing me.
The result should look
<ul> <li>parent <ul> <li> sub child <ul> <li> sub sub child</li> </ul> </li> </ul> </li> </ul>
I get
<ul> <li>parent <ul> <li> sub child </li> </ul> </li> </ul>
What am i still
template
<script type="text/template" id="template"> <ul class="parent"> {{#menu}} <li> {{item}} <ul class="popupmenu"> {{#menu}} <li>{{item}}</li> {{/menu}} </ul> </li> {{/menu}} </ul> </script>
Js
var data = { menu : [{ "item" : "parent", "menu" : [{ "item": "sub child", "menu": [{ "item" : "sub sub child" }] }] }] }; var template = $("#template").html(); var rendered = Mustache.render(template, data); $("body").html(rendered);
Fiddle
https://jsfiddle.net/6g7wz5vL/22/
In any case, I thought the mustache would recursively create helper lists based on the original template. But it seems to me that I should create an entire nested HTML structure to make it generate HTML for the next levels.
So what should I do a template for each level? What am I missing, that I only get the first sub-level and not the second? Or is it simply impossible with a Mustache?
javascript recursion mustache
Kris hollenbeck
source share