I get a json from database, then load dynamically a template and apply that json. The data stored in this variable is self-referenced (the typical id – parent_id).
I’m unable to do a recursion of that data.
the json:
var my_json = {"modules":[
{
"id_modules": "1",
"id_modules_parent": "0",
"mod_name": "mod 1",
"sub_modules": [
{
"id_modules": "2",
"id_modules_parent": "1",
"mod_name": "mod 1a",
"sub_modules": ""
},
{
"id_modules": "3",
"id_modules_parent": "1",
"mod_name": "mod 1b",
"sub_modules": ""
}
]
},
{
"id_modules": "4",
"id_modules_parent": "0",
"mod_name": "mod 2",
"sub_modules": [
{
"id_modules": "5",
"id_modules_parent": "4",
"mod_name": "mod 2a",
"sub_modules": [
{
"id_modules": "7",
"id_modules_parent": "5",
"mod_name": "mod 2aa",
"sub_modules": ""
}
]
},
{
"id_modules": "6",
"id_modules_parent": "4",
"mod_name": "mod 2b",
"sub_modules": ""
}
]
}
]}
This is how I render the template:
$.get("template_file.txt", function(template)
{
$.tmpl(template, my_json).appendTo("#some_div");
});
and finally the template (template_file.txt):
<ul class="modules_ul">
{{each(i, module) modules}}
<li>${module.mod_name}</li>
{{each(j, submodule) module.sub_modules}}
{{tmpl(submodule) ".modules_ul" }} //what goes here?
{{/each}}
{{/each}}
</ul>
Can anybody help me? Thanks in advance!
Edit:
added a jsfiddle to play around
SOLUTION: see mblase75’s answer in this post
It should be like this