液体和算术

时间:2012-12-24 20:51:23

标签: pagination jekyll liquid

我正在进行一些分页,我想知道是否有办法告诉液体仅显示5页。我正在寻找的输出是

<<前5 6 7 8 9最后>>

我目前使用的逻辑有效,但它显示了所有30个页面。

{% 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成为动态,我试过了

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

然而,如果paginator.page = 5,则此代码实际上不进行数学运算,那么循环为5..5并且不提供预期结果。我可以找出逻辑,以便它不会出现负数而且按预期工作但是我怎么能在这里做数学方程?

4 个答案:

答案 0 :(得分:4)

您需要使用paginator.total_pages上的过滤器进行算术运算,然后使用capture标记将结果捕获到变量中。获得开始和结束页面后,您可以像往常一样编写for循环:

{% 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 %}

答案 1 :(得分:1)

我正在与杰基尔建立一个博客,我遇到了类似的情况。根据我在Liquid wiki中找到的内容,可以使用limitoffset迭代给定集合的子集。

以下示例反映了您的特定情况, 应该在每个页面中正常工作,从第一个到最后一个:

{% capture start %}{{ paginator.page | minus: 3 }}{% endcapture %}

{% for i in (1..paginator.total_pages) limit: 5 offset: start %}
    ...
{% endfor %}

答案 2 :(得分:1)

我在我的网站上使用Bootstrap 3.0.3。我使用以下代码进行分页。 它具有您正在寻找的相同效果。我上面发布的代码是您正在寻找的代码,但无论如何我都会发布我的引导分页代码。

{% if paginator.total_pages != 1 %} 
{% if paginator.total_pages < 7 %}
        <div class="page-body col-md-12">
          <ul class="pagination pagination-centered">
            {% if paginator.total_pages >= 10 %} 
            {% if paginator.previous_page %}
                <li>
                  <a href="{{ site.url }}/">&laquo;&laquo;</a>
                </li>
            {% else %}
              <li class="disabled">
                <a>&laquo;&laquo;</a>
              </li>
            {% endif %}
            {% endif %}
            {% if paginator.previous_page %}
              {% if paginator.previous_page == 1 %}
                <li>
                  <a href="{{ site.url }}/">&laquo;</a>
                </li>
              {% else %}
                <li>
                  <a href="{{ site.url }}/page{{paginator.previous_page}}">&laquo;</a>
                </li>
              {% endif %}
            {% else %}
              <li class="disabled">
                <a>&laquo;</a>
              </li>
            {% endif %}
            {% if paginator.page == 1 %}
              <li class="active">
                <a>1</a>
              </li>
            {% else %}
              <li>
                <a href="{{ site.url }}/">1</a>
              </li>
            {% endif %}
            {% for count in (2..paginator.total_pages) %}
              {% if count == paginator.page %}
                <li class="active">
                  <a>{{count}}</a>
                </li>
              {% else %}
                <li>
                  <a href="{{ site.url }}/page{{count}}">{{count}}</a>
                </li>
              {% endif %}
            {% endfor %}
            {% if paginator.next_page %}
              <li>
                <a href="{{ site.url }}/page{{paginator.next_page}}">&raquo;</a>
              </li>
            {% else %}
              <li class="disabled">
                <a>&raquo;</a>
              </li>
            {% endif %}
            {% if paginator.total_pages >= 10 %} 
            {% if paginator.next_page %}
              <li>
                <a href="{{ site.url }}/page{{paginator.total_pages}}">&raquo;&raquo;</a>
              </li>
            {% else %}
              <li class="disabled">
                <a>&raquo;&raquo;</a>
              </li>
            {% endif %}
            {% endif %}
          </ul>
        </div>
{% else %}

{% assign page_start = paginator.page | minus: 2 %}
{% assign page_end = paginator.page | plus: 2 %}
{% if page_end > paginator.total_pages %}
{% assign page_end = paginator.total_pages %}
{% assign page_start = paginator.page | minus: 4 %}
{% endif %}
{% if page_start < 2 %}
{% assign page_end = paginator.page | plus: 3 %}
{% assign page_start = paginator.page | minus: 1 %}
{% endif %}
{% if page_start == 0 %}
{% assign page_end = paginator.page | plus: 4 %}
{% assign page_start = paginator.page %}
{% endif %}

<div class="page-body col-md-12">
    <ul class="pagination pagination-centered">
    {% if paginator.total_pages > 5 %} 
    {% if paginator.previous_page %}
        <li>
          <a href="{{ site.url }}/">&laquo;&laquo;</a>
        </li>
    {% else %}
      <li class="disabled">
        <a>&laquo;&laquo;</a>
      </li>
    {% endif %}
    {% endif %}
    {% if paginator.previous_page %}
      {% if paginator.previous_page == 1 %}
        <li>
          <a href="/">&laquo;</a>
        </li>
      {% else %}
        <li>
          <a href="/page{{paginator.previous_page}}">&laquo;</a>
        </li>
      {% endif %}
    {% else %}
      <li class="disabled">
        <a href="#">&laquo;</a>
      </li>
    {% endif %}
    {% if page_start == 1 %}
    {% assign page_end = paginator.page | plus: 4 %}
    {% assign page_start = 2 %}
    {% if paginator.page == 1 %}
      <li class="active">
        <a href="#">1</a>
      </li>
    {% else %}
      <li>
        <a href="/">1</a>
      </li>
    {% endif %}
    {% endif %}
    {% for count in (page_start..page_end) %}
      {% if count == paginator.page %}
        <li class="active">
          <a href="#">{{count}}</a>
        </li>
      {% else %}
        <li>
          <a href="/page{{count}}">{{count}}</a>
        </li>
      {% endif %}
    {% endfor %}
    {% if paginator.next_page %}
      <li>
        <a href="/page{{paginator.next_page}}">&raquo;</a>
      </li>
    {% else %}
      <li class="disabled">
        <a href="#">&raquo;</a>
      </li>
    {% endif %}
    {% if paginator.total_pages > 5 %} 
    {% if paginator.next_page %}
      <li>
        <a href="{{ site.url }}/page{{paginator.total_pages}}">&raquo;&raquo;</a>
      </li>
    {% else %}
      <li class="disabled">
        <a>&raquo;&raquo;</a>
      </li>
    {% endif %}
    {% endif %}
  </ul>
</div>
{% endif %}
{% endif %}

答案 3 :(得分:0)

试试这个

{% if paginator.total_pages < 7 %}
{% if paginator.page == 1 %}
        <span class="current bold">1</span>
{% else %}
        <a href="{{ site.url }}/">1</a>
{% endif %}
{% for count in (2..paginator.total_pages) %}
    {% if count == paginator.page %}
        <span class="current bold">{{ count }}</span>
    {% else %}
        <a href="/page{{ count }}/" class="pagenavi-page" title="{{ count }}">{{ count }}</a>
    {% endif %}
{% endfor %}
{% else %}
{% assign page_start = paginator.page | minus: 2 %}
{% assign page_end = paginator.page | plus: 2 %}
{% if page_end > paginator.total_pages %}
{% assign page_end = paginator.total_pages %}
{% assign page_start = paginator.page | minus: 4 %}
{% endif %}
{% if page_start < 2 %}
{% assign page_end = paginator.page | plus: 3 %}
{% assign page_start = paginator.page | minus: 1 %}
{% endif %}
{% if page_start == 0 %}
{% assign page_end = paginator.page | plus: 4 %}
{% assign page_start = paginator.page %}
{% endif %}
{% if page_start == 1 %}
{% assign page_end = paginator.page | plus: 4 %}
{% assign page_start = 2 %}
{% if paginator.page == 1 %}
        <span class="current bold">1</span>
{% else %}
        <a href="{{ site.url }}/">1</a>
{% endif %}
{% endif %}
{% for count in (page_start..page_end) %}
    {% if count == paginator.page %}
        <span class="current bold">{{ count }}</span>
    {% else %}
        <a href="/page{{ count }}/" class="pagenavi-page" title="{{ count }}">{{ count }}</a>
    {% endif %}
{% endfor %} 
{% endif %}