2

I have the following kind of array.

[{
  tag:'a',
  href:"#",
  title:"test",
  children:[{
         tag:"div",
         id:"nested-image",
         children:[{
                    tag:"img",
                    src:"image.jpg"
                   }]
         }]
  }]
}]

For rendering it I am using Underscore templates. I have used text plugin for loading templates

I am using following function for rendering.

  compiledTpl: function(tpl, data) {
        var data = data || {};
        var compiled = _.template(tpl);
        return compiled(data);
    },

From one of the templates I'm calling following function like this:

Inside the template I am iterating data object:

 <%_.each(data),function(d){%>
       <p>d.a</p>
       <p>d.b</p>
       <%if(d.children.length>0){%>
        //call same template itself
        <%= _.compiledTpl(tpl1,{'data':data} ) %>//not sure how to do that
      <%}%>
 <%}%>

If the function calls itself and everything is fine then it will not iterate through all the children of first level as in only one hierarchy will be there. It will not iterate through all children. I want to achieve something like this http://techdive.in/java/recursive-templates-underscore-js

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Priya
  • 1,453
  • 4
  • 29
  • 55
  • hey this should be helpful http://stackoverflow.com/questions/8067291/run-template-in-template-recursion-within-underscore-js-template-engine – StateLess Mar 25 '15 at 11:50
  • @ezpura i have nested children attribute. if i use that approach it would work fine for only one level and i dont know how many nested children it would have so i cant go for iteative apporach . thats why recursion is required. – Priya Mar 25 '15 at 11:56
  • @ezpura i want to do something like this url . http://techdive.in/java/recursive-templates-underscore-js – Priya Mar 25 '15 at 12:12
  • Where are you storing `compiledTpl`? Is it really a property of the `_` object? – Bergi Mar 25 '15 at 12:47
  • compiledTpl is part of underscore.mixin file which i am using – Priya Mar 25 '15 at 12:51

0 Answers0