Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquid and Arithmetic

I am working on some pagination and I am wondering if there is a way to tell liquid to only show 5 pages. The output I am looking for is

<< First 5 6 7 8 9 Last >>

The logic I currently have in place works but it is showing all 30 some pages.

{% for count in (2..paginator.total_pages) %}
    {% if count == paginator.page %}
        <span class="current">{{ count }}</span>
    {% else %}
        <a href="/page/{{ count }}/" class="pagenavi-page" title="{{ count }}">{{ count }}</a>
    {% endif %}
{% endfor %}

I would like to be able to make the 2 and paginator.total_pages be dynamic, I have tried

{% for count in ((paginator.page - 2)..(paginator.page + 2)) %} 

This code however does not actually do the math, if paginator.page = 5 then the loop is 5..5 and does not provide the expected results. I can figure out the logic so that it does not hit negative numbers and works as expected but how can I do math equations in this?

like image 535
Joseph Crawford Avatar asked Dec 24 '12 20:12

Joseph Crawford


1 Answers

You need use a filter on paginator.total_pages to do the arithmetic, and then capture the result in a variable using the capture tag. Once you have the start and end pages, you can write the for loop as you normally would:

{% capture page_start %}{{ paginator.page | minus: 2 }}{% endcapture %}
{% capture page_end %}{{ paginator.page | plus: 2 }}{% endcapture %}

{% for count in (page_start..page_end) %}
     {% comment %} ... do your thing ... {% endcomment %}
{% endfor %}
like image 107
seliopou Avatar answered Oct 29 '22 00:10

seliopou