访问jekyll分页中其他页面的帖子

时间:2016-12-21 14:29:32

标签: pagination jekyll

Jekyll分页使您可以在页面上整齐地排列帖子。我想把日期范围作为工具提示放在页面的链接上,就像:

myForm.validate({
  rules: {
   myCustomRule: {
      required: true
   }
  },
  messages {
   myCustomRule: {
     required: "Input values cannot be identical"
   }
  }
});

这样做的显而易见的方法是在页面上显示第一篇和最后一篇文章的日期。不幸的是,jekyll-pagination似乎只通过<li class="pager-item"> <a href="page-2" title="News from Sep 24 to Sep 29">Page 2</a> </li> 提供了当前页面的帖子列表。

也许我无法在documentation中找到它,所以我问你:是否可以通过jekyll分页中的其他页面访问帖子列表?像paginator.posts

这样的东西

另一种方法是构建我自己的paginaton,这应该没问题,但我更喜欢使用内置函数。

3 个答案:

答案 0 :(得分:1)

您可以间接获取信息:

  • paginator.previous_pagepaginator.next_page分别为您提供上一页和下一页的页码,如果页面不存在,则为nil
  • paginator.per_page为您提供每页的帖子数。
  • paginator.total_posts为您提供网站中的帖子总数。
  • site.posts为您提供所有帖子的反向时间顺序列表。

因此,鉴于当前paginator对象确保paginator.next_page != nil,您知道下一个分页页面中的帖子已在site.postsab的索引(包括在内):

a = (paginator.next_page) * paginator.per_page
b = a + paginator.per_page - 1

最后的分页页面可能不完整:请记住检查b < paginator.total_posts

确保paginator.previous_page != nil,以前的分页页面也是如此:

a = (paginator.previous_page) * paginator.per_page
b = a + paginator.per_page - 1

答案 1 :(得分:1)

我目前的做法如下:

{% if include.page > 0 and include.page <= paginator.total_pages %}
   {% assign first = include.page | minus:1 | times:paginator.per_page %}
   {% assign last = paginator.per_page | times:include.page | minus:1  %}
   {% if last > paginator.total_posts %}
      {% assign last = paginator.total_posts | minus:1 %}
   {% endif %}
   {{ site.posts[first].date | date: "%d.%m.%Y" }} bis  {{ site.posts[last].date | date: "%d.%m.%Y" }}
{% else %}
   OUT_OF_RANGE
{% endif %}

我真的不喜欢Liquid-Syntax:D

答案 2 :(得分:0)

或者:

{% comment %} NOTE : shortcut "p" for "paginator" {% endcomment %}
{% assign p = paginator %}

{% comment %} date formating (see shorthand formats in Ruby's documentation : http://ruby-doc.org/core-2.3.3/Time.html#method-i-strftime) {% endcomment %}
{% assign dateFormat = "%y %b %d" %}

<ul>
{% for post in p.posts %}<li><a href="{{ post.url }}">{{ post.title }} - {{ post.date | date: dateFormat }}</a></li>
{% endfor %}
</ul>

{% comment %}++++++++++ if previous page == newest posts {% endcomment %}
{% if paginator.previous_page %}
  {% assign prevPage = p.page | minus: 1 %}
  {% assign prevPageLastIndex = prevPage | times: p.per_page | minus: 1  %}
  {% assign prevPageFirstIndex = prevPageLastIndex | minus: p.per_page | plus: 1 %}
  {% assign prevPagefirstPostDate = site.posts[prevPageFirstIndex].date | date: dateFormat %}
  {% assign prevPagenextPageLastPostDate = site.posts[prevPageLastIndex].date | date: dateFormat %}

  {% capture prevPageLink %}
  <p><a href="{{ paginator.previous_page_path }}"></p>
  Newest posts from
    {% if prevPagefirstPostDate != prevPagenextPageLastPostDate %}
      {{ prevPagenextPageLastPostDate }} to {{ prevPagefirstPostDate }}
    {% else %}
      {{ prevPagenextPageLastPostDate }}
    {% endif %}
  </a>
  {% endcapture %}

{% endif %}

{% comment %} ++++++++++ if next page = oldest posts {% endcomment %}
{% if paginator.next_page %}
  {% assign nextPage = p.page | plus: 1 %}
  {% assign nextPageFirstIndex = p.page | times: p.per_page %}

  {% comment %}>>Next page is not the last page = normal computing {% endcomment %}
  {% if nextPage != p.total_pages %}
    {% assign nextPageLastIndex = nextPageFirstIndex | plus: p.per_page | minus: 1 %}

  {% comment %}>>Next page is the last page compute index from p.total_posts{% endcomment %}
  {% else %}
    {% assign nextPageLastIndex = p.total_posts | minus: 1 %}

  {% endif %}

  {% assign nextPagefirstPostDate = site.posts[nextPageFirstIndex].date | date: dateFormat %}
  {% assign nextPageLastPostDate = site.posts[nextPageLastIndex].date | date: dateFormat %}

  {% capture nextPageLink %}
  <p><a href="{{ paginator.next_page_path }}"></p>
  Previous posts from
    {% if nextPagefirstPostDate != nextPageLastPostDate %}
      {{ nextPageLastPostDate }} to {{ nextPagefirstPostDate }}
    {% else %}
      {{ nextPageLastPostDate }}
    {% endif %}
  </a>
  {% endcapture %}

{% endif %}

{{ prevPageLink }}
{{ nextPageLink }}