Jac*_*die 20 filter liquid jekyll
有没有办法按字母顺序对一些帖子进行排序,使用Jekyll?
我现在有这样的事情:
{% for post in site.categories.threat %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
它有效,但帖子混乱了.如果他们按字母顺序排序,我认为会更好看.
谢谢
Chr*_*cht 23
它可以在没有插件的情况下完成,这意味着它可以与Github Pages一起使用.
但是,你必须使用一些丑陋的字符串操作技巧.
我使用类似的方法来实现标记页面(列出每个标记的所有帖子).
同样的方法,略有修改:
{% 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 %}
Run Code Online (Sandbox Code Playgroud)
在第一个循环中需要两个不同的分隔符(当然在split
稍后的调用中也是如此).
为了使其工作,任何帖子标题或URL都不得出现这两个字符!!
我正在使用|
,#
在这个例子中,它适用于我(我刚刚用我的博客测试过它).但您可能需要使用不同的字符,具体取决于您的帖子标题以及URL的构造方式.
如果您只想显示某个标签/类别(而不是所有帖子)中的帖子,您可以将第一个for
循环(其中的一个capture
)更改为以下其中一个:
{% for post in site.tags['whatever'] %}
{% for post in site.categories['whatever'] %}
Run Code Online (Sandbox Code Playgroud)
Arn*_*ier 14
根据文档,要通过其中一个字段过滤数组,您可以使用:
{% assign sortedPosts = site.posts | sort: 'title' %}
Run Code Online (Sandbox Code Playgroud)
然后sortedPosts
变量将包含已排序的数组.
文档可以在这里找到:https://docs.shopify.com/themes/liquid/filters/array-filters#sort
Ton*_*nas 10
在没有插件的情况下在GitHub页面中对Jekyll进行排序既干净又优雅.在_data目录中使用.yml数据文件.我在这里使用一个名为的数据文件team-members.yml
:
team-members.yml
此模式将处理您需要在此处执行的操作.
小智 5
我从https://gist.github.com/3812259改编了一个Jekyll插件来实现这一目标.我无法按原样使用插件,因为它在存在空值时失败.我是一个初学的ruby程序员,并在/sf/answers/56610501/的帮助下对空值处理进行编码
sort_for示例反转排序并执行区分大小写的字符串比较(如果sorted属性不是字符串,则忽略):
{% sorted_for node in site.pages reversed sort_by:title case_sensitive:true %}
{{ node.title }}
{% endsorted_for %}
Run Code Online (Sandbox Code Playgroud)
sorted_keys_for示例:
{% 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 %}
Run Code Online (Sandbox Code Playgroud)
要在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)
Run Code Online (Sandbox Code Playgroud)