9 Pagination
Hugo Soucy edited this page 3 months ago

Pagination

If you want to split a long list of entries in several pages, you can create a pagination from an index.lua file. Add the pagination property with a table as value:

return {
  date = "2013-09-07",
  datetime = "10:00:00",
  template = "portfolio-index",
  title = "Illustrations",
  pagination = {}
}

By default there will be 6 entries by page and the URLs of those pages will have the pg- prefix. But you can change that by adding limit and the prefix properties to the pagination table:

pagination = {
  limit = 10,
  prefix = 'p'  
}

The entries that will be paginated are the subpages of the directory where pagination is requested. You can change that with the collection property to produce a pagination of a specific location:

return {
  date = "2013-09-07",
  datetime = "10:00:00",
  template = "portfolio-index",
  title = "Illustrations",
  pagination = {
    limit = 10,
    prefix = 'p'  
  },
  collection = { 'portfolio/' },
}

A index template with pagination

<div>
  <% if children then %>
    <% if pagination and pagination.length > 1 then %>
      <% local relprev = rellink..pagination.prefix..tostring(pagination.current - 1)..'.html' %>
      <% local relnext = rellink..pagination.prefix..tostring(pagination.current + 1)..'.html' %>

      <nav class="nav-pagination nav-sequential">
        <ul>          
          <li class="nav-sequential__prev">
            <a
              class="nav-pagination__prev"
              <% if pagination.current > 1 then %>
              href="<%= pagination.current > 2 and relprev or rellink -%>"
              rel="prev"
              <% end %>>
              Pécédent
            </a>
          </li>
          <li class="nav-sequential__next">
            <a
              class="nav-pagination__next"
              <% if pagination.current < pagination.length then %>
              href="<%= relnext -%>"
              rel="next"
              <% end %>>
              Suivante
            </a>
          </li>
        </ul>
      </nav>
    <% end %>

    <ol class="portfolio-list">
      <% for i = 1, #children do  %>
        <li class="portfolio-list__item">
          <a href="/portfolio/<%= children[i].slug -%>.html">
            <img
              alt=""
              src="/images/portfolio/<%= children[i].imagename:match('(.+)%..*')..'-320'..children[i].imageext -%>"
              width="320" />
          </a>
        </li>
      <% end %>
    </ol>
  <% end %>
</div>