You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.8 KiB

  1. # Paged Plugin for DocPad
  2. This plugin provides [DocPad](https://docpad.org) with Paging. Documents can declare a number of pages that should be rendered for the document, or a collection over which the document should be rendered repeatedly.
  3. ## Install
  4. ```
  5. npm install --save docpad-plugin-paged
  6. ```
  7. ## Usage
  8. ### Setup
  9. To use, simply add `isPaged: true` to the meta data for any document that you want to be rendered with paging. You can then use `pageSize` or `pagedCollection` to instruct the plugin how many pages to generate from this document.
  10. In documents that have paging enabled we can use the `@document.page` object to retrieve information about the current and total pages. The `page` object is of the form:
  11. ```
  12. {
  13. number: 0, // current page number
  14. count: 10, // total number of pages
  15. size: 5, // expected number of documents per page
  16. startIdx: 0, // document index for first document in this page
  17. endIdx: 5 // document index for last document in this page
  18. }
  19. ```
  20. ### Paging Collections
  21. You can generate pages over a collection by using the `pagedCollection` meta property. Simply specify the name of a collection and the plugin will use this collections length to calculate how many pages to generate from this document. So if your `posts` collection contains 5 documents, and you have specified a `pageSize` of 3 then the paging plugin will render 2 pages, the first with documents 0-2 and the second with the remaining 2 documents.
  22. ### Example
  23. For instance we could create the file `src/documents/index.html.eco` which contains something similar to the followìng:
  24. ```
  25. ---
  26. title: 'Home'
  27. layout: 'default'
  28. tags: ['page']
  29. isPaged: true
  30. pageOrder: 0
  31. pagedCollection: 'posts'
  32. pageSize: 3
  33. ---
  34. <% posts = @getCollection('posts') %>
  35. <% for i in [@document.page.startIdx...@document.page.endIdx]: %>
  36. <% document = posts.at(i).toJSON() %>
  37. <article id="post" class="post">
  38. <h1><a href='<%=document.url%>'><%= document.title %></a></h1>
  39. <div class="post-date"><%= document.date.toLocaleDateString() %></div>
  40. <div class="post-content">
  41. <%- document.contentRenderedWithoutLayouts %>
  42. </div>
  43. </article>
  44. <% end %>
  45. <div class="pagination">
  46. <ul>
  47. <% if !@getDocument().hasPrevPage(): %>
  48. <li class="disabled"><span>Prev</span></li>
  49. <% else: %>
  50. <li><a href="<%= @getDocument().getPrevPage() %>">Prev</a></li>
  51. <% end %>
  52. <% for num in [0..@document.page.count-1]: %>
  53. <% if @document.page.number == num: %>
  54. <li class="active"><span><%= num %></span></li>
  55. <% else: %>
  56. <li><a href="<%= @getDocument().getPagedUrl(num) %>"><%= num %></a></li>
  57. <% end %>
  58. <% end %>
  59. <% if !@getDocument().hasNextPage(): %>
  60. <li class="disabled"><span>Next</span></li>
  61. <% else: %>
  62. <li><a href="<%= @getDocument().getNextPage() %>">Next</a></li>
  63. <% end %>
  64. </ul>
  65. </div>
  66. ```
  67. This will render out documents from the `posts` collection in groups of 3. The first 3 documents in the collection will be rendered into a file called `index.html` as normal, then the remaining documents from the collection will be rendered into subsequent files `index.1.html`, `index.2.html`, `index.3.html` etc.
  68. In this example we can also see the use of the new helper methods `hasPrevPage()`, `hasNextPage()`, `getPrevPage()`, `getNextPage()` and `getPagedUrl(n)`. Hopefully these methods are pretty self explanatory, with the get methods returning the urls for the relevant pages. In this example we use these methods to create the standard Twitter Bootstrap pagination HTML.
  69. ## History
  70. You can discover the history inside the `History.md` file
  71. ## License
  72. Licensed under the incredibly [permissive](http://en.wikipedia.org/wiki/Permissive_free_software_licence) [MIT License](http://creativecommons.org/licenses/MIT/)
  73. <br/>Copyright &copy; 2012 [Ben Delarre](http://delarre.net)