2016-03-28 30 views
1

我在Jekyll中有一個我想要排序的集合。按標題排序當然很容易。按照Liquid和Jekyll中的修改變量排序

<ul> 
{% for note in site.note | sort: "title" %} 
<li>{{note.path | git_mod }}: {{ note. title }}</li> 
{% endfor %} 
</ul> 

我想按日期排序。但由於集合沒有日期,所以我有一個自定義的Liquid過濾器,該過濾器採用該項目的路徑,並在Git中獲取其上次修改的時間。你可以在上面的代碼中看到,我將路徑傳遞給git_mod。我可以驗證這是否有效,因爲當我列出列表時,我會得到正確的最後修改時間,而且是完整日期。 (實際上,我也將它傳遞給date_as_string。)

但是我無法按該值排序,因爲Liquid不知道它,因爲它已經在site.note集合中的每個項目中都有值。我怎樣才能按照這個價值來分類?我想這樣的事情,但它不工作:

<ul> 
{% for note in site.note | sort: path | date_mod %} 
<li>{{note.path | git_mod }}: {{ note. title }}</li> 
{% endfor %} 
</ul> 

我也試着像變種:{% for note in site.note | sort: (note.path | git_mod) %}

這些都不拋出一個錯誤,但他們沒有工作,要麼。

回答

1

這是一種您可以使用Jekyll hooks的情況。

您可以通過git_mod關鍵

{% assign sortedNotes = site.note | sort: 'git_mod' %} 
{% for note in sortedNotes %} 
.... 

注創建_plugins/git_mod.rb

Jekyll::Hooks.register :documents, :pre_render do |document, payload| 

    # as posts are also a collection only search Note collection 
    isNote = document.collection.label == 'note' 

    # compute anything here 
    git_mod = ... 

    # inject your value in dacument's data 
    document.data['git_mod'] = git_mod 

end 

然後,您就可以進行排序,你不能sort在for循環中。你首先需要sortassign,然後loop

+0

謝謝。這很好。 'jekyll build --incremental'似乎失去了重建後的日期,但這並不是這個答案的錯。 –