使用液体按字母顺序分类帖子

26

有没有一种使用Jekyll按字母顺序对多篇文章进行排序的方法?

我现在有类似这样的东西:

{% for post in site.categories.threat %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}

代码可以正常运行,但是帖子的顺序很混乱。我认为如果按字母顺序排序会更美观。

谢谢。

7个回答

24

可以不使用插件完成,这意味着它可以与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 %}

注意:

在第一个循环内(当然后面的split调用也要),您需要使用两个不同的分隔符字符
为了使其起作用,这两个字符都不能出现在任何文章标题或URL中!!

我在本例中使用 | 和 #,这对我有效(我刚刚在我的博客上测试过)。但是,根据您的文章标题和URL的构造方式,您可能需要使用不同的字符。


额外奖励:

如果您只想显示特定标签/类别中的帖子(而不是所有帖子),您可以将第一个for循环(即capture内部的循环)更改为以下之一:

{% for post in site.tags['whatever'] %}

{% for post in site.categories['whatever'] %}

我有一个问题,关于一个相似的事情,请看此处链接:http://stackoverflow.com/questions/35707352/how-to-group-data-by-first-letter-from-a-csv-file-in-liquid。 - Miloš Miljković
使用“|”进行分割后,数组的第一个条目为空,因此我在输出周围包裹了“{% unless forloop.first %} {% endunless %}”以忽略它。 - SeanFromIT

22

11

使用 Jekyll 在 GitHub Pages 中进行排序既干净又优雅,而不需要插件。在 _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 %}

这个模式将处理你在这里需要做的事情。


1
这对我非常有效,我用它来处理我的帖子标题。 - tomohulk

6
我从https://gist.github.com/3812259中改编了一个Jekyll插件来实现这个功能。我不能直接使用原始插件,因为它无法处理空值。作为一名初级的ruby程序员,我在https://dev59.com/9nRA5IYBdhLWcg3w1BmW#808721的帮助下编写了空值处理代码。
例如对于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)

1
这正是我需要的简单目录表,谢谢! - Rob N

3

为了日后参考,我想要补充以下内容。

如果你想按标题排序帖子,可以使用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) - Tony Brasunas

0

我在本地站点测试了Christian的好方法:输出中有一个空链接(我不知道为什么),因此第一个链接无法工作,所以我修改了他的代码,在<a href={{ postitems[1] }}">{{ postitems[0] }}</a><br>这行之前插入了{% if postitems[1] %},并在之后插入了{% endif %}。建议参考tanky woo's comment


-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接