Handlebars: recursive tree structure
Asked Answered
G

2

7

I have a tree structure of arbitrary depth that I want to display with Handlebars. I don't see any way to recurse. If I knew the depth, I could hard code it I suppose, but it can be arbitrarily deep.

Something like this, but it needs to recurse at the display children part.

{{#aNode}}
    {{id}
    {{name}}
    {{description}}
    ...spew this same template with each member of {{#children}}...
{{/aNode}}

Handlebars has ways to iterate collections, but no way that I can see to recurse into the children

Gainer answered 3/10, 2013 at 22:26 Comment(0)
G
6

Found that you can do it with a delegation/embedded file technique. So, it'd look like this:

spew_a_node.mustache (I'm using the Mustache implementation):

{{#aNode}}
    {{id}
    {{name}}
    {{description}}
    {{#children}}
        {{> spew_a_node}}
    {{/children}}    
{{/aNode}}
Gainer answered 8/10, 2013 at 20:22 Comment(0)
S
4

Here's a nice article and jsfiddle describing exactly how to do it (which is more or less what Chris Kessel has described in his answer):

http://www.boduch.ca/2014/03/recursive-list-building-with-handlebars.html

Shortwinded answered 11/6, 2014 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.