How to create templates for content collections or channels with pagination?

Hello,

How to create templates for content collections or channels with pagination?

I have followed this article: https://ghost.org/tutorials/creating-content-collections/

Suppose I have a collection called tweets with tag hash-tweet. I have assigned it in routes.yaml file like this:
collections:
/tweets/:
permalink: /tweets/{slug}/
template: tweets
filter: tag:hash-tweet

Then I created a template file called tweets.hbs with this:
{{#get “posts” include=“tags,authors” visibility=“all” filter=“tags:hash-tweet” limit=“all”}}
{{#foreach posts order=“published_at desc” limit=@config.posts_per_page}}
{{> “loop”}}
{{/foreach}}
{{/get}}
{{pagination}}

I have tried it both for collections and channels. Everything is working but pagination is not working well. On the second or third page, it shows the same posts as the first page. If I remove the limit in “foreach” it shows all posts on the first page but I want to split them with pagination. The same thing happens in channels. Please help me out with this.

I am using Ghost version: 3.38.2 and a custom theme.

Thank you!
Anisul

1 Like

The get helper is used to fetch additional data. Depending on the context, each template will be supplied with some data (e.g. the single post, a list of posts, a list of tags, etc.). Since you’re using a collection (i.e. an index of posts), the template should use the foreach helper to loop over the list of posts.

Thank you @vikaspotluri123 for your response.
I have removed get helper from the code:
{{#foreach posts}}
{{title}}
{{/foreach}}
Still no luck. It is working well on the main index or home page and paginated posts but not on the collections and channels index page. It shows the pagination but not get any title, every page is blank.

Can you try using the tweets template for the home page? That way we can see if it’s a handlebars issue or a data issue :slight_smile:

At last, I have found the solution. It is routes.yaml file. I have transferred home index “/:” to routes and all collections are now working well. Previously home index “/:” was in collections.
Thank you @vikaspotluri123

1 Like

can you post your site URL? I’d like to see a tweet page!

Actually the real problem is not ‘/’ route in the collection, but it being the first one. So what you have to do is to move the / route to the bottom one in the collection.
This should be specified in the documentation