无法通过自定义jekyll集合进行分组

Gil*_*das 2 group-by filter liquid jekyll

我正在尝试使用其中一个元数据(日期)的值来对自定义jekyll集合中的元素进行分组.所以,我正在做,{% for elt in site.my_collection | group_by: "date" %}但它正常循环通过集合,就像我写的那样{% for elt in site.my_collection %}.

更奇怪的是,如果我在模板中书写{{ site.my_collection | group_by: "date" }},那么它会正确显示分组的集合[{"name" => "day1", "items" => [#, #, #]}, {"name" => "day2", "items" => [#]}].

我究竟做错了什么?这是因为我正在使用自定义集合吗?

谢谢.

Dav*_*uel 5

你不能loop and sort/group同时.

你必须assign and sort/group 那么做 loop

这对于Jekyll元素(如页面,帖子或集合)来说都是如此.

{% assign collection = site.my_collection | group_by: "date" %}

{% for group in collection %}
  <h3>{{ group.name | date: "%-d %B %Y" }}</h3>
  <ul>
    {% for item in group.items %}
    <li>{{item.data}}</li>
    {%endfor%}
  </ul>
{%endfor%}
Run Code Online (Sandbox Code Playgroud)