而是一个理论性的问题-如何在Meteor中呈现递归模板?例如,显示对注释的注释,其中注释子级别的数量不受限制,以便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>
递归的一个简化示例,假设您有一个以下示例模板:
<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
,然后打印该注释及其所有子注释等。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句