液体和算术 [英] Liquid and Arithmetic
问题描述
我正在做一些分页,我想知道是否有一种方法可以告诉液体只显示5页.我正在寻找的输出是
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
<<前5 6 7 8 9后>>
<< First 5 6 7 8 9 Last >>
我目前使用的逻辑有效,但是它显示了全部30个页面.
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 %}
我希望能够使2和paginator.total_pages动态化,我已经尝试过
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)) %}
但是,此代码实际上并未进行数学运算,如果paginator.page = 5,则循环为5..5,并且不提供预期的结果.我可以弄清楚逻辑,这样它就不会达到负数并且可以按预期工作,但是我该怎么做数学方程式呢?
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?
推荐答案
您需要在paginator.total_pages
上使用过滤器进行算术运算,然后使用capture
标记将结果捕获到变量中.一旦有了起始页和结束页,就可以像通常那样编写for
循环:
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 %}
这篇关于液体和算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!