按前题数组值列出 collection 中的项目
List items from a collection by front matter array values
我有一个带有 collection、colleges
的 Jekyll 站点,我想按自定义 rankings
前言属性对其进行排序和显示。以下是 collection:
中的项目示例
---
title: Example College
score: 88
rankings:
- top accredited colleges
- most affordable
---
...
---
title: Sample College
score: 75
rankings:
- most affordable
- best online programs
---
...
---
title: Example University
score: 75
rankings:
- top accredited colleges
- best online programs
---
我想将不同的排名显示为标题,下面列出了大学:
top accredited colleges
- Example College
- Example University
most affordable
- Example College
- Sample College
best online programs
- Sample College
- Example University
我试过像这样使用 Liquid 的 group_by
过滤器:
{% assign groups = site.colleges | group_by: "rankings" %}
{% for group in groups %}
<h2>{{ group.name }}</h2>
<ul>
{% for college in group.items %}
<li>{{ college.title }}</li>
{% endfor %}
</ul>
{% endfor %}
但这似乎是通过完整数组的精确匹配对 rankings
前面的内容进行分组,而不是将它包含的不同值分开:
<h2>[top accredited colleges, most affordable]</h2>
<ul>
<li>Example College</li>
</ul>
<h2>[most affordable, best online programs]</h2>
<ul>
<li>Sample College</li>
</ul>
<!-- etc -->
如何从输出中的 rankings
前题数组中分离出各个值?
这可以解决问题:
{% assign allRankings = site.colleges | map: "rankings" %}
{% assign rankingArray = "" | split:"" %}
{% for rankings in allRankings %}
{% assign rankingArray = rankingArray | concat: rankings | uniq %}
{% endfor %}
{% for ranking in rankingArray %}
<h2>{{ ranking | capitalize }}</h2>
<ul>
{% for college in site.colleges %}
{% if college.rankings contains ranking %}
<li>{{ college.title }}</li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
我有一个带有 collection、colleges
的 Jekyll 站点,我想按自定义 rankings
前言属性对其进行排序和显示。以下是 collection:
---
title: Example College
score: 88
rankings:
- top accredited colleges
- most affordable
---
...
---
title: Sample College
score: 75
rankings:
- most affordable
- best online programs
---
...
---
title: Example University
score: 75
rankings:
- top accredited colleges
- best online programs
---
我想将不同的排名显示为标题,下面列出了大学:
top accredited colleges
- Example College
- Example University
most affordable
- Example College
- Sample College
best online programs
- Sample College
- Example University
我试过像这样使用 Liquid 的 group_by
过滤器:
{% assign groups = site.colleges | group_by: "rankings" %}
{% for group in groups %}
<h2>{{ group.name }}</h2>
<ul>
{% for college in group.items %}
<li>{{ college.title }}</li>
{% endfor %}
</ul>
{% endfor %}
但这似乎是通过完整数组的精确匹配对 rankings
前面的内容进行分组,而不是将它包含的不同值分开:
<h2>[top accredited colleges, most affordable]</h2>
<ul>
<li>Example College</li>
</ul>
<h2>[most affordable, best online programs]</h2>
<ul>
<li>Sample College</li>
</ul>
<!-- etc -->
如何从输出中的 rankings
前题数组中分离出各个值?
这可以解决问题:
{% assign allRankings = site.colleges | map: "rankings" %}
{% assign rankingArray = "" | split:"" %}
{% for rankings in allRankings %}
{% assign rankingArray = rankingArray | concat: rankings | uniq %}
{% endfor %}
{% for ranking in rankingArray %}
<h2>{{ ranking | capitalize }}</h2>
<ul>
{% for college in site.colleges %}
{% if college.rankings contains ranking %}
<li>{{ college.title }}</li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}