簡體   English   中英

使用液體按字母順序對帖子進行排序

[英]Using liquid to sort posts alphabetically

是否可以使用Jekyll按字母順序對一些帖子進行排序?

我現在有這樣的事情:

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

它有效,但帖子混亂。 我認為如果按字母順序對它們進行排序會更好。

謝謝

無需插件即可完成,這意味着它可以與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的構造方式。


獎金:

如果您只想顯示某個標簽/類別中的帖子(而不是所有帖子) ,則可以將第一個for循環capture中的一個更改為以下其中一個:

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

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

根據文檔,要按其字段之一過濾數組,可以使用:

    {% assign sortedPosts = site.posts | sort: 'title' %}

然后sortedPosts變量將包含已排序的數組。

可以在這里找到文檔: https : //docs.shopify.com/themes/liquid/filters/array-filters#sort

在沒有插件的情況下在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 %}

此模式將處理您在此處需要執行的操作。

我從https://gist.github.com/3812259改編了Jekyll插件來完成此任務。 我不能按原樣使用該插件,因為它在存在空值時失敗。 我是一名紅寶石程序員,並在https://stackoverflow.com/a/808721/1135052的幫助下編碼了null處理

sort_例如反轉排序並執行區分大小寫的字符串比較(如果sorted屬性不是字符串,則忽略):

{% 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)

我想添加以下內容以供將來參考。

要按標題對帖子進行排序,可以使用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 %}

沒有插件或自定義功能就無法完成。 雖然,正在不斷努力以在下一個版本中實現此目的: https : //github.com/Shopify/liquid/pull/101 ,然后看起來像:

{% for tag in site.tags order:ascending %} 
   ...
{% endfor %}

另請參閱: 使用Jekyll /液體模板訂購陣列

我在本地站點上測試了Christian的出色解決方案:在第一行之前,輸出中有一個空鏈接(我不為什么),因此第一個鏈接不起作用,因此我修改了他的代碼,插入{% if postitems[1] %}在行<a href={{ postitems[1] }}">{{ postitems[0] }}</a><br>以及{% endif %}之后,建議了tanky woo的評論

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM