如何在流星中做递归模板?
问题描述:
相当理论上的问题 - 我如何渲染流星中的递归模板?例如,显示评论的评论,其中包含无限数量的评论子级别,以便将HTML显示为以下内容?如何在流星中做递归模板?
<section>
some text
<section>
nested text
<section>
further nested text and sections
.....
</section>
</section>
</section>
在我来说,我通过“树”模板MongoDB的文件,这个文件可以有子含量水平的数量不受限制。我的下面的例子不按我想要的方式工作。
<template name="tree">
<div class="wrapper" style="border:1px solid red">
<ul>
{{#each getStructure}}
<li>
{{#each content}}
<ul>
<li>
<a class="item">{{text}}</a>
<!-- TODO: this stuff needs to be recursive.
{{#if sub_content}}
<ul>
{{#each sub_content}}
<li>
<a class="item">{{text}}</a>
{{#if sub_content}}
....
{{/if}}
</li>
{{/each}}
</ul>
{{/if}}
</li>
</ul>
{{/each}}
</li>
{{/each}}
</ul>
</div>
</template>
答
recirsuve的一个简单的例子,假设你有一个职位样本模板:
<template name="post">
{{post_text}}
{{#each comments}}
{{>comment}}
{{/each}}
</template>
和后帮手:
Template.post.helpers({
comments: function() {
return CommentCollection.find({post_id: this._id, parent_id: {$exists: 0}});
}
});
我想创建一个模板的注释布局,并提供一个帮助,其中的子注释取决于您的数据结构,如下所示:
<template name="comment">
{{comment_text}}
{{#each sub_comment}}
{{> comment}}
{{/each}}
</template>
,然后沿的线条助手:
Template.comment.helpers({
sub_comments: function() {
return CommentCollection.find({parent_id: this._id});
}
});
这将递归地为每个子评论评论模板然后回滚上树下一#each
然后打印该评论和所有的其子评论等
谢谢亚伦。你的回复是现货,但是我发现我的问题不是模板本身,而是应用程序崩溃在复杂的数据上。你似乎是Meteor和Hanlebars的知识分子 - 你能否看看我的另一个问题? http://stackoverflow.com/questions/27364206/meteor-throws-exception-exception-from-tracker-recompute-function-too-much-rec – lekso 2014-12-08 18:16:00
这个演示非常有帮助。 – ergusto 2015-08-21 16:00:08