Paginate

From Shopify Wiki

(Redirected from Pagination)
Jump to: navigation, search

The paginate tag is responsible for pagination within Shopify. It is currently applicable to collections, products, blog articles and search results.

By default, Shopify limits the number of products shown in a collection to 50. If you want to show more products you will need to use the paginate tag.

Themes you will see listed on the Theme Gallery page all use paginate for collections and blog pages.

Contents

Usage

To use pagination you have to wrap your for loop in which you iterate over a collection in the pagination tag. The pagination tag will figure out all the metrics by itself and make sure that further calls to the collection will return with the correct items.

Image:paginated-collection.png

 {% paginate collection.products by 5 %}  
   {% for product in collection.products %}
     {{ product.title }}
   {% endfor %}
  {% endpaginate %}

The paginate object

Within the paginate block you have access to the paginate object which provides the following information:

  • paginate.page_size - The size of each page. That's the amount of items displayed.
  • paginate.current_page - On which page are we right now?
  • paginate.current_offset - How many items did we skip over so far
  • paginate.pages - The amount of pages there are
  • paginate.items - Total amount of items in this collection
  • paginate.previous - Exists if there is a previous page.
    • paginate.previous.title - Title of the link
    • paginate.previous.url - URL of the link
  • paginate.next - Exists if there is another page.
    • paginate.next.title - Title of the link
    • paginate.next.url - URL of the link
  • paginate.parts - Array of all the parts which make up a good navigation for this pagination. Each element will have any of these three elements:
    • part.is_link - Is this part a link?
    • part.title - Link Title
    • part.url - Link URL

Displaying the pagination navigation

Manually using pagination

  {% if paginate.previous %}
    {{ paginate.previous.title | link_to: paginate.previous.url }}
  {% endif %}
  {% for part in paginate.parts %}
    {% if part.is_link %}
      {{ part.title | link_to: part.url }}
    {% else %}
      {{ part.title }}        
    {% endif %}        
  {% endfor %}      
  {% if paginate.next %}
    {{ paginate.next.title | link_to: paginate.next.url }}
  {% endif %}


This will give you:

<a href="/collections/works-on-paper?page=1" title="">« Previous</a>
<a href="/collections/works-on-paper?page=1" title="">1</a>
<a href="/collections/works-on-paper?page=3" title="">Next »</a>

If you want to change the link text, replace paginate.previous.title with 'Link Text' i.e.

  {% if paginate.previous %}
    {{ 'previooooooous' | link_to: paginate.previous.url }}
  {% endif %}

The easy way:

The paginate tag will add a new paginate variable to your view from which all vital parameters for pagination can be extracted.

If you just want to get a good standard pagination going, you can take this paginate variable and pipe it into the filter default_pagination which will leave you with attractive looking and fully functional pagination.

{% paginate collection.products by 5 %}  
  {% for product in collection.products %}
    {{ product.title }}
  {% endfor %}

  <div id="pagination">
  {{ paginate | default_pagination }}
  </div>
{% endpaginate %}

Default pagination

This is the way default pagination shows up in your source code.

<p>
<span class="page current">1</span> 
<span class="page"><a href="/search?page=2&q=slant" title="">2</a></span> 
<span class="next"><a href="/search?page=2&q=slant" title="">Next »</a></span>
</p>

Showing which items are displayed

To output something like this:

Showing items 26-50 of 345.

Use this:

Showing items {{ paginate.current_offset | plus: 1 }}-{% if paginate.next %}{{ paginate.current_offset | plus: paginate.page_size }}{% else %}{{ paginate.items }}{% endif %} of {{ paginate.items }}.