这让我发疯.
我有这个系列resources:
# _config.yml
collections:
resources:
output: true
permalink: /resources/:name/
Run Code Online (Sandbox Code Playgroud)
他们都有约会:
# /_resources/example.md
---
title: Learn the Web
date: 09-04-2013
---
Run Code Online (Sandbox Code Playgroud)
页面生成,如果我尝试显示它的日期,它会正确显示,但我也想按日期排序,但它不起作用.我究竟做错了什么?
{% assign sortedResources = site.resources | sort: 'date' %} <!-- Doesn't work -->
{% for resource in sortedResources %}
<div>
{{resource.title}}
<small>{{resource.date | date: "%d %b %Y"}}</small> <!-- Works -->
</div>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我正在使用:
? ruby --version
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-linux]
? jekyll --version
jekyll 2.5.3
Run Code Online (Sandbox Code Playgroud)
谢谢
我目前遇到了与收藏相同的问题.
虽然试图理清欧洲日期格式像dd/mm/yyyy或者dd-mm-yyyy,我得到一个字符串排序.即使timezone: Europe/Paris在_config.yml文件中设置了.
获取按日期排序的集合的唯一方法是使用ISO格式yyyy-mm-dd.
# /_resources/example.md
---
title: Learn the Web
date: 2013-04-09
---
Run Code Online (Sandbox Code Playgroud)
现在这种情况正在发挥作用.
编辑 - 这是jekyll管理'日期'的方式:
date: "2015-12-21" # String
date: 2015-12-1 # String D not zero paded
date: 01-12-2015 # String French format
date: 2015-12-01 # Date
date: 2015-12-21 12:21:22 # Time
date: 2015-12-21 12:21:22 +0100 # Time
Run Code Online (Sandbox Code Playgroud)
如果您不需要时间,您可以坚持使用date: YYYY-MM-DD格式.而且你必须在整个系列中保持一致.如果你混合String,Date和/或Time Liquid会抛出一个像Liquid error: comparison of Date with Time failed或的错误Liquid error: comparison of String with Date failed
如果您的收藏品在前面的内容中具有有效date(ISO 8601格式),则它们将按日期自动排序,最早排序.
如果您想首先输出更多最近的项目,您可以reverse这样订购:
{% assign sorted = site.resources | reverse %}
{% for item in sorted %}
<h1>{{ item.name }}</h1>
<p>{{ item.content }}</p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我明白了:资源按日期字符串(例如19-06-2015)排序,这是不正确的。
我创建了自定义过滤器:
# _plugins/filters.rb
module Jekyll
module DateFilter
require 'date'
def date_sort(collection)
collection.sort_by do |el|
Date.parse(el.data['date'], '%d-%m-%Y')
end
end
end
end
Liquid::Template.register_filter(Jekyll::DateFilter)
Run Code Online (Sandbox Code Playgroud)
像这样使用:
{% assign sortedResources = site.resources | date_sort | reverse %}
{% for resource in sortedResources %}
<div>{{resource.title}}</div>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3045 次 |
| 最近记录: |