Search code examples
docpad

Get the next and previous file in collection with docpad


I'm making a portfolio and want to simply print a next & previous project link on each project detail page using DocPad (which uses Backbone collections). The code here is from the my template projects.html.eco. The @document object is the document currently being viewed.

<% for document in @getCollection('projects').toJSON() : %>

 <% if document.url.indexOf('/posts') is @document.url.indexOf('/projects') + 1: %>
   <a href="<%= document.url %>" class="next"><img src="/images/rt_arrow.png" alt="" /></a>
 <% end %>

 <% if document.url.indexOf('/posts') is @document.url.indexOf('/projects') - 1: %>
   <a href="<%= document.url %>" class="previous"><img src="/images/lft_arrow.png" alt="" /></a>
 <% end %>

<% end %>

Let me know if I can provide any more information!

Thank you!


Solution

  • Seems like there are a few things going on:

    1. There is a combination of /posts and /projects being used in your comparison, I'm going with they are both mean to be /projects due to the name of the collection you are cycling.

    2. The + 1 and - 1 you current have only apply to the index position of the /projects string in the url, rather than the index of the actual document.

    For a solution for paging, there is currently this gist available, which should help for your use case. In the future, a plugin could be done up to provide the simply next() and prev() jquery style API you're after.