2

我正在尝试实现一种使用 jsRender 递归模板的方法。问题是,我的数据对象具有一元自引用层次结构,需要递归循环来显示所有属性。这里的例子是一个论坛帖子,可以包含任意数量的回复帖子。每个回复帖子可以包含任意数量的子帖子等。除了递归部分,我有以下代码工作。我只能通过谷歌找到一个对此的远程引用,所以这是我迄今为止所拥有的:

<script id="forumPostsTemplate" type="text/x-jsrender">
    <article class="forumPost">
        <header class="forumPostHeader">{{:PostSubject}}
            <div class="info">By: Some Person, {{:CreatedDate}} <a href="">Flag as innapropriate</a> </div>
        </header>
        <div class="content">
        {{:PostContent}} 
        {{for Replies}}
            {{:Replies tmpl="#forumPostsTemplate"}}
        {{/for}}
        </div>
    </article>
</script>

有没有人有这种功能的经验?如果有帮助,我目前正在运行最新版本的 jsRender。

4

2 回答 2

2

根据 jsRender 的这个例子,这样调用你的模板是否有效?

https://github.com/BorisMoore/jsrender/blob/master/demos/step-by-step/06_template-composition.html

 {{for Replies tmpl="#forumPostsTemplate"/}}
于 2012-05-07T22:58:52.013 回答
0

这三个不同版本的树标记控件正好说明了这种递归: http:
//www.jsviews.com/#samples/tag-controls/tree

于 2012-05-08T04:39:16.770 回答