Jekyll排序为字符串,应该是一个日期

时间:2014-12-18 17:00:32

标签: jekyll prose

我正在将Jekyll与Prose结合使用,并设置了一些名为pub_date的元数据。

在散文中,这被设置为文本字段(尚不支持日期时间字段)

用户输入类似于2015-01-23的pub_date,我可以使用此值并通过日期方法运行它以正确输出日期(例如{{ post.pub_date | date: "%b %-d, %Y"}}有效)

当我尝试对这些值进行排序时,它们会被视为字符串;

{% assign sorted_posts = (paginator.posts | sort: 'pub_date', 'first') %}

我有更好的方法来整理这个系列吗?或者我有什么办法可以强迫价值像约会一样?

我正在使用github页面来托管解决方案,所以不幸的是我们无法对Jekyll做任何定制。

2 个答案:

答案 0 :(得分:2)

更正语法,以paginator.posts排序pub_date

{% assign sorted_posts = paginator.posts | sort: 'pub_date' %}

{% for post in sorted_posts %}
  <h1><a href="{{ post.url }}">{{ post.title }}</a></h1>
  <p class="author">
    <span class="date">Pubdate : {{ post.pub_date | date: "%b %-d, %Y"}}</span>
  </p>
{% endfor %}

我不知道最后的first是否应该获得数组的第一个帖子,但在这种情况下,它是:

{% assign sorted_posts = paginator.posts | sort: 'pub_date' | first %}

---> as we get one post NO loop !

<h1><a href="{{ post.url }}">{{ sorted_posts.title }}</a></h1>
<p class="author">
<span class="date">Pubdate : {{ sorted_posts.pub_date | date: "%b %-d, %Y"}}</span>
</p>

答案 1 :(得分:1)

使用带引号的字符串作为排序参数时出错。 Jekyll :: Post与Jekyll :: Post失败的比较

相反,使用以下方法。

{% assign sorted_posts = paginator.posts | sort: :pub_date | reversed %}
{% assign latest_post = sorted_posts | last %}

<!-- do something with latest post -->

{% for post in sorted_posts reversed %}
{% if forloop.first %}<!-- discard the first post -->{% else %}

<!-- iterate over posts -->

{% endif %}
{% endfor %}

不是100%确定为什么我们需要两次调用反向,但它有效。