OK, total rewrite of my answer, I'll leave the old stuff below for historical reference to the troubleshooting steps.
According to this: Liquid and Arithmetic Liquid has it's own way of capturing variables and doing arithmetic. Thanks to the OP for the find. Just surprised that I couldn't find this in their own documentation.
OK, since Liquid apparently needs and
, not &&
I would try getting the arithmetic out of the if statement.
{% for page in (1..paginator.total_pages) %}
{% assign two_less = (paginator.page - 2) %}
{% assign two_more = (paginator.page + 2) %}
{% if page == paginator.page %}
({{ page }})
{% elsif page >= two_less and page <= two_more %}
{{ page }}
...
I think you might be seeing a problem with the Liquid template language. There have been problems before with and
in Liquid, even in strings. See this closed issue: https://github.com/Shopify/liquid/issues/13
I would try this:
{% elsif (page >= (paginator.page - 2)) && (page <= (paginator.page + 2)) %}
and
and &&
are slightly different in ruby, and possibly Liquid has some special significance for and
.
old answer
It is basically doing exactly what you tell it to do. You say in the comments "I know the value is 8 because it prints 16 times". That is not valid debugging. All you know is that
page = 8 #at the time of execution
and that your if
statement determined that it needed to print that value every time. So you are assuming that "8" is coming from the various branches of your if
statement, but it is far more likely that your if
statement keeps evaluating to the branch that prints "8". The simplest debugging you can do here is add:
{% if paginator.total_pages > 1 %}
{% for page in (1..paginator.total_pages) %}
{% if page == paginator.page %}
({{ page }})
{{ puts "if" }}
{% elsif page >= (paginator.page - 2) and page <= (paginator.page + 2) %}
{{ page }}
{{ puts "else if" }}
{% else %}
{{ paginator.page }}
{{ puts "else" }}
{% endif %}
{% endfor %}
{% endif %}
I don't use Jekyll so I am not 100% sure of my syntax, but you see what I'm trying to achieve? This will tell you where in the if
statement your output value is coming from. If it is coming from the arithmetic as you suspect, it will tell you that. What I suspect though, is that you will see it never gets into that part of the if
as you assume. Also, I'm not sure why you are getting
8 8 8 8 8 6 7 (8) 9 10 8 8 8 8 8 8 8
Do you want the first five and the last seven elements to evaluate to 8
and not:
1 2 3 4 5 6 7 (8) 9 10 11 12 13 14 15 16 17
Also just to be sure you're getting the type of variable return you expect change
(paginator.page - 2) and page <= (paginator.page + 2)
to:
(paginator.page.to_i - 2) and page <= (paginator.page.to_i + 2)
So my answer is pretty much what mcfinnigan said, arithmetic with variables does work. So you are either not getting an integer from the method paginator.page
, or there is some other flaw in the if
statement's logic.
edit----- (removed part about "paginator.number" as it doesn't seem to apply to user's case)
I can expand and improve this answer if you can do the debugging I suggested and post the output.