Pagination with custom article collections?

I created a category custom collection:

  blog.custom_collections = {
    category: {
      link: "/categories/{category}.html",
      template: "/category.html"
    }
  }

No matter what I try, when I add a pagination to the category template, it does not work, but it works on the tag page as well as on the index page. blog.paginate is set to true.

thanks!

1 Like

I encountered this problem myself and overcame it by using dynamic pages. I’ve so far come up with the following code to help solve the problem. I’m sure there is room for improvement in it, but it will give you an idea.

# In config.rb
def get_locals(category, total_pages, i, articles)
  prev_page = false

  # Is the a previous page and if so what does it look like
  if (i - 1) == 1
    prev_page = "/categories/#{category}"
  elsif (i - 1) > 1
    prev_page = "/categories/#{category}/p/#{i-1}"
  end

  # Return the local variables
  {
    :page_articles => articles,
    :paginate => total_pages > 1,
    :category => category,
    :page_number => i,
    :num_pages => total_pages,
    :next_page => if (i + 1) <= total_pages then "/categories/#{category}/p/#{i+1}" else false end,
    :prev_page => prev_page
  }
end

ready do
  per_page = 5 #Set the number of posts per page here

  # Loop through all articles and group them by category and then loop through each group
  blog.articles.group_by {|a| a.data.category }.each do |category, articles|

    total_pages = articles.size.fdiv(per_page).ceil # Calculate the numer of pages

    page_articles = articles.slice(0, per_page) # Get the articles for the page

    # Create a proxy for the main/first category page
    proxy "/categories/#{category}", "/category.html", :locals => get_locals(category, total_pages, 1, page_articles)

    # Create pages for each of the pagination
    (1..total_pages).each do |i|
      page_articles = articles.slice((i - 1) * per_page, per_page)
      proxy "/categories/#{category}/p/#{i}", "/category.html", :locals => get_locals(category, total_pages, i, page_articles)
    end

  end

end

You may want to customise the links, something to note is the style I’ve used is sensitive to the presence of trailing slashes.

1 Like

Inside Middleman source paginator is initialized after posts and tags (these page types paginated as well), but before custom_collections, so paginator isn’t affected to categories, for instance. I don’t know why this not changed yet. I just created pull-request for enabling paginator for custom collections https://github.com/middleman/middleman-blog/pull/228, hope it’ll help.

Thanks!

This looks great. Anyone have a status update? I noticed that it’s still not implemented on the master branch…

Hey Kyle! This functionality already in master branch. I see two releases here, both are in beta. But you can use gem 'middleman-blog', '~> 3.6.0.beta.2' in your Gemfile before final release. Hope it helps.

Awesome, that works wonderfully. Thank you, good sir. :slight_smile:

Thanks! That’s exactly what I needed. I don’t know why middleman-blog doesn’t include the same functionality as for tags (list/index and pagination) by default. This solved it for me.

If anyone else faces this issue, I still haven’t migrated to Middleman 4, so I’ve isolated this specific issue on top of middleman-blog v3.5.3 at this branch.

You can use it by changing this line in your Gemfile:

gem "middleman-blog", "~> 3.5.3", github: 'manastech/middleman-blog', branch: 'v3.5.3-paginate-custom-collections'

Hope it helps!