【问题标题】:Is it possible do sort Jekyll Collection and Jekyll Data in one array?是否可以在一个数组中对 Jekyll Collection 和 Jekyll Data 进行排序?
【发布时间】:2016-10-31 10:43:50
【问题描述】:
我怎样才能对这两个for 语句混合的<ul> 进行排序?
我已经尝试过使用 javascript 和 jquery,但我只得到<ul> 是NULL 的错误。
<ul>
{% assign docs = site.documents | sort: "title" %}
{% for document in docs %}
<li>
<a href="{{ document.url }}">{{ document.title }} - {{ document.url }}</a>
</li>
{% endfor %}
{% assign dataDocs = site.data.documents | sort: "title" %}
{% for document in dataDocs %}
<li>
<a href="{{ document.url }}" download="{{ document.path }}">{{ document.title }} - {{ document.url }}</a>
</li>
{% endfor %}
</ul>
【问题讨论】:
标签:
javascript
jquery
sorting
jekyll
liquid
【解决方案1】:
您正在对每个列表而不是一个列表进行排序。您可以在排序之前将其设为一个列表,并通过为每个项目提供一个额外的前端标识符 (is_from_data) 以不同方式呈现每个项目:
<ul>
{% assign docs = site.documents | concat: site.data.documents | sort: "title" %}
{% for document in docs %}
<li>
{% if document.is_from_data %}
<a href="{{ document.url }}" download="{{ document.path }}">{{ document.title }} - {{ document.url }}</a>
{% else %}
<a href="{{ document.url }}">{{ document.title }} - {{ document.url }}</a>
{% endif %}
</li>
{% endfor %}
</ul>
您可以将is_from_data: true 添加到每个项目的前面,或者在您的配置中使用 Jekyll 默认值。
旧版本的 Jekyll 可能没有 concat 过滤器。您可以通过将每个元素推送到新数组来解决此问题:
{% assign docs = "" | split: "" %}
{% for doc in site.documents %}
{% assign docs = docs | push: doc %}
{% endfor %}
{% for doc in site.data.documents %}
{% assign docs = docs | push: doc %}
{% endfor %}