把手:递归树结构

时间:2013-10-03 22:26:17

标签: handlebars.js mustache reentrancy

我想要用Handlebars显示任意深度的树结构。我没有看到任何回复的方法。如果我知道深度,我想我可以硬编码,但它可以任意深入。

像这样的东西,但它需要在显示子部分递归。

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

Handlebars有办法迭代集合,但是我无法看到它们可以递归到孩子们身上

2 个答案:

答案 0 :(得分:6)

发现您可以使用委托/嵌入式文件技术来完成此操作。所以,它看起来像这样:

spew_a_node.mustache(我正在使用Mustache实现):

{{#aNode}}
    {{id}
    {{name}}
    {{description}}
    {{#children}}
        {{> spew_a_node}}
    {{/children}}    
{{/aNode}}

答案 1 :(得分:4)

这里有一篇很好的文章和jsfiddle,描述了如何做到这一点(这或多或少是Chris Kessel在他的回答中所描述的):

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