遍历 Jekyll 中的单个 _data 文件并对结果进行分组

Looping through a single _data file in Jekyll and grouping the results

设置:目前我有一个脚本可以读取 google 电子表格并将结果写入 GitHub 页面站点上 _data 文件夹中的 *.yaml 文件。

典型的结果如下所示:

- ip: 192.168.0.1
  mac: 'a4:13:4e:5e:2c:c0'
  hostname: XWR-3100V1 Wireless Router
  vendor: Luxul
  dns_name: XWR-3100.lan
  mdns_name: test
  smb_name: test
  smb_domain: test
  comments: test
  type: Router
- ip: 192.168.0.4
  mac: 'b0:6e:bf:9c:11:00'
  hostname: Toshiba eStudio 4505ac
  vendor: ''
  dns_name: ''
  mdns_name: ''
  smb_name: ''
  smb_domain: ''
  comments: ''
  type: Printer

从那里,我创建了一个显示 table 的页面,该页面循环显示信息并显示所有设备。

我无法理解以下语法:

查看数据并识别所有不同类型(路由器、媒体播放器、打印机)等

创建 table 类型为组 header,然后在 header 下方列出与该类型匹配的记录,然后继续下一个类型,依此类推上。

单个 yaml 文件是否可行,或者我是否需要 some-how 生成另一个仅包含在第一个文件中找到的类型列表的文件?

谢谢!

{% assign devices = site.data.network %}
{% for ip in devices %} //not sure what this really means
{{ip.ip}} | {{ip.mac}} ...etc.
{% endfor %}

为简洁起见,我排除了 table 标签。

使用group_by过滤器:

{% assign net = site.data.network | group_by:"type" %}

{% for type in net %}
  <h1>{{ type.name }}</h1>
  <ul>
    {% for item in type.items %}
    <li>{{ item.ip }} ...</li>
    {% endfor %}
  </ul>
{% endfor %}