我想显示一个只显示不同类别的菜单。
假设有以下结构:
_folder1
- com1.html
- com2.html
- com3.html
现在,让我只关注三个文件。
对于_folder1中包含的每个文件,您有以下YAML MATTER
标题:1File
类型:y
项目:1
标题:2File
类型:y
项目:1
标题:3File
类型:y
项目:2
现在,我想展示以下列表:
项目- 1 2
和我不想要双1
在Jekyll中达到它的最佳实践是什么?
这是可能的,但是您需要一些非常丑陋的字符串操作技巧来实现它。
据我所知,在Liquid中没有合适的方法来自己创建数组。
因此,下面90%的解决方案都是为了创建数组而滥用字符串。
<!-- Step 1: create an array with all projects (with duplicates) -->
{% for page in site.pages %}
{% if page.project %}
{% capture tmp %}{{ tmp }}#{{ page.project }}{% endcapture %}
{% endif %}
{% endfor %}
{% assign allprojects = tmp | remove_first: '#' | split: '#' | sort %}
<!-- Step 2: create an array of unique projects (without duplicates) -->
{% for project in allprojects %}
{% unless tmp2 contains project %}
{% capture tmp2 %}{{ tmp2 }}#{{ project | strip }}{% endcapture %}
{% endunless %}
{% endfor %}
{% assign uniqueprojects = tmp2 | remove_first: '#' | split: '#' | sort %}
<!-- Step 3: display unique projects -->
<h1>Projects:</h1>
<ul>
{% for project in uniqueprojects %}
<li>{{project}}</li>
{% endfor %}
</ul>
最后,步骤3将生成以下HTML…完全符合要求:
<h1>Projects:</h1>
<ul>
<li>1</li>
<li>2</li>
</ul>