2016-03-28 11 views
1

私は並べ替えたいジキルにコレクションがあります。タイトルによる並べ替えはもちろん簡単です。LiquidとJekyllで変更された変数でソート

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

日付で並べ替えたい。しかし、コレクションには日付がないので、アイテムのパスを取得し、Gitで最後に変更された時刻を取得するカスタムのLiquidフィルターがあります。上記のコードでは、パスを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を使用できる場合です。

あなたは、その後のためのループであなたがすることができないsortこと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 

を作成することができます。最初にsortassign、次にloopにする必要があります。

+0

ありがとうございました。それはうまくいった。 'jekyll build --incremental'は再構築後の日付を失うようですが、これは確かにこの答えの誤りではありません。 –

関連する問題