【发布时间】:2015-03-19 11:01:20
【问题描述】:
我想遍历_data/sections/ 中的每个文件,但输出按所述文件中包含的数据排序(order 属性)。当前输出的顺序恰好是正确的,虽然我不知道为什么,并且在修改 sorted 属性时顺序确实没有改变。
文件结构如下:
// project/_data/sections/food.yml
title: Food
order: 2
content: "Food ipsum dolor sit amet."
-----
// project/_data/sections/drink.yml
title: Drink
order: 1
content: "Drink ipsum dolor sit amet."
按照Jekyll docs for data files上的结构,for循环代码如下:
// project/index.html
// ...
{% for section_hash in site.data.sections | sort: 'order' %}
{% assign section = section_hash[1] %}
<p><strong>{{ section.title }}</strong> - {{ section.content }}</p>
{% endfor %}
// ...
我还尝试在将这些部分传递给 for 循环 as seen here 之前对它们进行排序:
{% assign sections_sorted = sita.data.sections | sort: 'order' %}
{% for section in sections_sorted %}
<p><strong>{{ section.title }}</strong> - {{ section.content }}</p>
{% endfor %}
最后,我尝试将order属性移动到_data/sections/中每个节文件的front-matter,但这导致了异常:Liquid Exception: no implicit conversion of String into Integer
// project/_data/sections/drink.yml
---
order: 1
---
title: Drink
content: "Drink ipsum dolor sit amet."
这对于_data/ 子目录中的文件是否可行?如何按order 对这些文件的输出进行数字排序,按title 反向字母排序,等等?
【问题讨论】:
标签: sorting for-loop jekyll liquid