Get next and previous file in a collection using docpad

I am creating a portfolio and just want to print the next and previous project link on each project details page using DocPad (which uses the Backbone collections). The code here is from my projects.html.eco template. The @document object is the document 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 more information!

Thanks!

+4
source share
1 answer

It seems that several things are happening:

  • Your comparison uses a combination of /posts and /projects , I am going with them to be /projects because of the name of the collection you ride on the bike.

  • The + 1 and - 1 tags that you use apply only to the index position of the /projects in the URL, and not to the index of the actual document.

For a paging solution, there is currently this meaning that should help in your use case. In the future, a plugin could be made to provide a simple next() and prev() jquery style API, which you are after.

+1
source

Source: https://habr.com/ru/post/1436509/


All Articles