yorbro
yorbro

Reputation: 1137

Loop counter when rendering with {{render}}

I have a list of posts that I render in a loop.

{{#each post in posts}}
  {{render "post" post}}
{{/each}}

Now, in the post template, I would like to access the index of the post in order to output "post 1", "post 2" and so on. How can I achieve that?

I tried accessing contentIndex from each PostView but that property is null.

Upvotes: 1

Views: 79

Answers (1)

Marcio Junior
Marcio Junior

Reputation: 19128

Inside of your post template you can use {{_view._parentView.contentIndex}} to access the index, it's zero relative. Like this sample JSFiddle.

Other alternative can be wrap your posts content in a new object with the index. Like this JSFiddle

Upvotes: 2

Related Questions