Search code examples
javascriptjsrender

jsRender Recursive Templating


I'm trying implement a way to recursively template using jsRender. The issue is, my data object has a unary self-referencing heirarchy which requires recursive looping to show all of the attributes. The example here is a forum post which can contain any number of reply posts. Each reply post can contain any number of children posts and so on. I have the following code working except for the recursion part. I could only find one remote reference to this via the Googles, so here is what I have thus far:

<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>

Does anyone have any experience with this sort of functionality? I am currently running the most recent version of jsRender if that helps.


Solution

  • Per this example for jsRender, does it work to call your template like this instead?

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

     {{for Replies tmpl="#forumPostsTemplate"/}}