Blaise
Blaise

Reputation: 22212

loop in jsRender

I know {{for}} can loop through a collection. But can I loop based on the total number?

Say, if I have a TotalPage which is an integer, how can I create a paging list like:

{{for (Page=1; Page<=TotalPage; Page++;)}} // I made up this statement.
    {{if Page=CurrentPage}}
        <li class="selected">{{>Page}}</li>
    {{else}}
        <li>{{>Page}}</li>
    {{/if}}
{{/for}}

Is that possible in jsRender?

Upvotes: 2

Views: 3904

Answers (2)

Lakshitha Udara
Lakshitha Udara

Reputation: 153

       {{for #data.pages}}
          {{if #data.page == currentPage }}  // data refers to the pages 
              <li class="selected">{{>Page}}</li>
          {{/if}}   
        {{else}}
               <li>{{>Page}}</li>   
        {{/for}}

Hope this is the structure that you are looking for ,

Upvotes: 0

Stephen Collins
Stephen Collins

Reputation: 3653

This can be accomplished with the #index attribute that exists within the {{for /}} block.

{{for myModel.myArray }}
    <li>This is item number {{:#index}}</li>
{{/for}}

#index contains the current index of the array being rendered. You cannot, however, at least natively, specify an arbitrary number of times to render a template. as Matt Ball says in the comments, that's too much logic to be putting in a template.

But if you're that determined, you can create an array in your model with an arbitrary size and iterate over that.

Upvotes: 2

Related Questions