【发布时间】:2012-02-18 00:06:22
【问题描述】:
有没有办法使用 Jekyll 按字母顺序对多个帖子进行排序?
我现在有这样的东西:
{% for post in site.categories.threat %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
它有效,但是帖子混乱了。我认为如果按字母顺序排序会更好看。
谢谢
【问题讨论】:
有没有办法使用 Jekyll 按字母顺序对多个帖子进行排序?
我现在有这样的东西:
{% for post in site.categories.threat %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
它有效,但是帖子混乱了。我认为如果按字母顺序排序会更好看。
谢谢
【问题讨论】:
在没有插件的情况下在 GitHub 页面中的 Jekyll 中排序既干净又优雅。使用 _data 目录中的 .yml 数据文件。我在这里使用了一个名为team-members.yml的数据文件:
{% assign sorted_team = site.data.team-members | sort:'title' %}
{% for member in sorted_team %}
<span class="title">{{ member.title }}</span>
{% endfor %}
此模式将处理您需要在此处执行的操作。
【讨论】:
根据文档,要按其中一个字段过滤数组,您可以使用:
{% assign sortedPosts = site.posts | sort: 'title' %}
那么sortedPosts 变量将包含排序后的数组。
文档可以在这里找到:https://docs.shopify.com/themes/liquid/filters/array-filters#sort
【讨论】:
我想添加以下内容以供将来参考。
要按标题对帖子进行排序,您可以使用sort 过滤器。
见http://jekyllrb.com/docs/templates/#filters
所以,这行得通:
{% assign sorted_threat_posts = site.categories.threat | sort: 'title', 'last' %}
{% for post in sorted_threat_posts %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
【讨论】:
{% assign sorted_team = site.data.team-members | sort: 'title', 'name' %} 但我得到编译错误:Liquid Exception: wrong number of arguments (3 for 2)
我在本地站点测试了 Christian 的出色解决方案:在第一行之前,输出中有一个空链接(我不知道为什么),因此第一个链接不起作用,所以我修改了他的代码插入 @987654322 @ 在<a href={{ postitems[1] }}">{{ postitems[0] }}</a><br> 行之前,{% endif %} 之后。建议tanky woo's comment。
【讨论】:
它可以在没有插件的情况下完成,这意味着它可以与Github Pages 一起使用。
不过,您必须使用一些难看的字符串操作技巧。
我使用了类似的方法to implement a tag page (that lists all posts for each tag)。
同样的方法,稍作修改:
{% capture posts %}
{% for post in site.posts %}
|{{ post.title }}#{{ post.url }}
{% endfor %}
{% endcapture %}
{% assign sortedposts = posts | split: '|' | sort %}
{% for post in sortedposts %}
{% assign postitems = post | split: '#' %}
<a href={{ postitems[1] }}">{{ postitems[0] }}</a><br>
{% endfor %}
您需要在第一个循环中两个不同的分隔符(当然在稍后的split 调用中也需要)。
为了为此,这两个字符不得出现在任何帖子标题或 URL 中!!
我在这个例子中使用了| 和#,这对我很有效(我刚刚用我的博客测试了它)。但您可能需要使用不同的字符,具体取决于您的帖子标题和 URL 的构造方式。
如果您只想显示某个标签/类别中的帖子(而不是所有帖子),您可以更改第一个for 循环(@987654329 内的那个) @) 到其中之一:
{% for post in site.tags['whatever'] %}
{% for post in site.categories['whatever'] %}
【讨论】:
{% unless forloop.first %} {% endunless %} 包裹在输出周围以忽略它。
我从https://gist.github.com/3812259 改编了一个 Jekyll 插件来完成这个任务。我无法按原样使用插件,因为它在存在空值的情况下失败。我是一名初级 ruby 程序员,在 https://stackoverflow.com/a/808721/1135052 的帮助下编写了 null 处理代码
sort_例如反转排序并执行区分大小写的字符串比较(如果排序的属性不是字符串则忽略):
{% sorted_for node in site.pages reversed sort_by:title case_sensitive:true %}
{{ node.title }}
{% endsorted_for %}
sorted_keys_例如:
{% sorted_keys_for tag in site.tags %}
<a href="/tags/{{ tag | downcase | replace:" ","-"}}.html">{{ tag }}</a><br />
Num posts: {{ site.tags[tag].size }}
{% endsorted_keys_for %}
为了在 Jekyll 中使用,将此代码放在 _plugins/sort_for.rb 中
module Jekyll
module SortedForImpl
def render(context)
sorted_collection = collection_to_sort context
return if sorted_collection.empty?
sort_attr = @attributes['sort_by']
case_sensitive = @attributes['case_sensitive'] == 'true'
i = sorted_collection.first
if sort_attr != nil
if i.to_liquid[sort_attr].instance_of? String and not case_sensitive
sorted_collection.sort_by! { |i|
k = i.to_liquid[sort_attr]
k ? k.downcase : ''
}
else
sorted_collection.sort_by! { |i|
k = i.to_liquid[sort_attr]
[k ? 1 : 0,k || 1]
}
end
else
if i.instance_of? String and not case_sensitive
sorted_collection.sort_by! { |i| i.downcase }
else
sorted_collection.sort!
end
end
original_name = @collection_name
result = nil
context.stack do
sorted_collection_name = "#{@collection_name}_sorted".sub('.', '_')
context[sorted_collection_name] = sorted_collection
@collection_name = sorted_collection_name
result = super
@collection_name = original_name
end
result
end
end
class SortedForTag < Liquid::For
include SortedForImpl
def collection_to_sort(context)
return context[@collection_name].dup
end
def end_tag
'endsorted_for'
end
end
class SortedKeysForTag < Liquid::For
include SortedForImpl
def collection_to_sort(context)
return context[@collection_name].keys
end
def end_tag
'endsorted_keys_for'
end
end
end
Liquid::Template.register_tag('sorted_for', Jekyll::SortedForTag)
Liquid::Template.register_tag('sorted_keys_for', Jekyll::SortedKeysForTag)
【讨论】:
没有插件或自定义功能就无法完成。虽然,在下一个版本中正在努力实现这一点:https://github.com/Shopify/liquid/pull/101,然后它看起来像:
{% for tag in site.tags order:ascending %}
...
{% endfor %}
【讨论】: