在Liquid和Jekyll中按修改的变量排序

在Liquid和Jekyll中按修改的变量排序,jekyll,liquid,Jekyll,Liquid,我有一个收集在杰基尔,我想排序。当然,按标题排序很容易 <ul> {% for note in site.note | sort: "title" %} <li>{{note.path | git_mod }}: {{ note. title }}</li> {% endfor %} </ul> 我也尝试过类似的变体:{%for note in site.note | sort:(note.path | git_mod)%} 所有这些都不会引发错

我有一个收集在杰基尔,我想排序。当然,按标题排序很容易

<ul>
{% for note in site.note | sort: "title" %}
<li>{{note.path | git_mod }}: {{ note. title }}</li>
{% endfor %}
</ul>
我也尝试过类似的变体:
{%for note in site.note | sort:(note.path | git_mod)%}


所有这些都不会引发错误,但也不会起作用。

这是一种可以使用的情况

您可以创建一个_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
然后,您将能够按
git\u mod
键进行排序

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

请注意,您不能在for循环中对进行排序。首先需要在
分配中
排序
,然后
循环

谢谢。这很有效
jekyll build——增量的
似乎在重建后丢失了日期,但这肯定不是这个答案的错误。您介意共享git_mod的代码吗?我也在试着做同样的事情
{% assign sortedNotes = site.note | sort: 'git_mod' %}
{% for note in sortedNotes %}
....