Jon Griffith
Jon Griffith

Reputation: 183

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

Setup: Currently I have a script that reads a google spreadsheet and writes the results to a *.yaml file in my _data folder on my GitHub pages site.

A typical results looks like this:

- 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

From there, I've created a page that displays a table that loops through the information and displays all of the devices.

I can't figure out the syntax for the following:

Look through the data and identify all of the different Types (Router, Media Player, Printer) etc.

Create the table with Type as the group header, and then list the records that match that type below the header, then move on to the next type, and so on.

Is this possible with a single yaml file, or do I need to some-how generate another file that contains ONLY the list of types found in the first file?

Thanks!

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

I excluded the table tags for brevity.

Upvotes: 0

Views: 235

Answers (1)

David Jacquel
David Jacquel

Reputation: 52809

Use group_by filter :

{% 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 %}

Upvotes: 1

Related Questions