按页面元数据过滤收集项 [英] Filter Collection Items by page metadata
本文介绍了按页面元数据过滤收集项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为product-categories
的jekyll集合,其中每个文件的开头都有以下元数据:
I have a jekyll collection called product-categories
in which each file has the following metadata in front matter:
_product-categories/filename1.md
---
- title
- uuid
---
我有一个页面,其首页的内容包含此集合中的文件名(集合数组选择通过其带有首页内容的文件名保存)...
I have a page whose front matter contains filenames from this collection (collection array selections are saved by their filenames with front matter)...
page.html
---
product-categories:
- filename1
- filename2
---
[list of product-categories to be displayed here]
目标
我想在页面上显示这些product-categories
的 title (来自集合元数据).由于项目是通过文件名保存在最前面的,所以这不可能吗?
Goal
I want to display the title (from the collection metadata) of these product-categories
on the page. Since the items are saved in the front matter by their filename, shouldn't this be possible?
推荐答案
您可以这样做:
{% comment %} --- Get product-categories collection's datas --- {% endcomment %}
{% assign collection = site.collections | where: "label", "product-categories" | first %}
{% comment %} --- collection's docs root path --- {% endcomment %}
{% assign collection_path = collection.relative_directory | append: "/" %}
<ul>
{% for cat in page.product-categories %}
{% comment %} --- expected file path --- {% endcomment %}
{% assign filepath = collection_path | append: cat | append:".md" %}
{% comment %} Look for files that have path == filepath.
As "where" filter return an array,
we pick the first and only item in array {% endcomment %}
{% assign file = site.product-categories | where:"path", filepath | first %}
{% if file %}
<li>{{ file.title }}</li>
{% else %}
{% comment %} --- error in front matter list ---{% endcomment %}
<li>No file match for <strong>{{ cat }}</strong> : file at <strong>{{ filepath }}</strong> not found</li>
{% endif %}
{% endfor %}
</ul>
这篇关于按页面元数据过滤收集项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文