Jekill按变量中指定的属性排序

时间:2017-08-06 07:15:09

标签: sorting jekyll liquid

我正在使用Jekill创建模板。是否可以按变量中指定的属性对项目进行排序,如下所示:

<!-- Collection identity -->
{% assign collection_id = page.id %}

<!-- Select pages and sort them by a value provided for the collection -->
{% assign product_pages = (site.pages | sort: [collection_id]) %}

排序不起作用(编译错误)。

任务是让一些页面定义带有标题的类别,如下所示:

---
layout: collection
id: sale
...
---

然后一些页面可能在一个或多个集合中,以便在每个collectino中定义它们的排序顺序,页面将有这样的标题:

---
layout: item
sale: 100
news: 30
general: 1000
...
---

Sale,News,General等具有应该自动生成的相同模板,并且它们可以具有已排序的集合。

1 个答案:

答案 0 :(得分:1)

是的,可以按属性对项目进行排序,但页面没有id属性,但您可以按其他属性对其进行排序,例如: name 。< / p>

未排序:

{% for p in site.pages %}
{{p.name}}
{% endfor %}

page.name排序:

{% assign product_pages = site.pages | sort: 'name' %}

{% for p in product_pages %}
{{p.name}}
{% endfor %}

更新

使用添加到页面的自定义id属性进行排序:

{% assign product_pages = site.pages | sort: 'id' %}