nemenems nemenems - 3 months ago 29
YAML Question

With jekyll / liquid how to get all keys of a yml hash

I have this yaml

param1:
key1: value1
key2: value2

param2:
key1: value1
key2: value2

param3:
key1: value1
key2: value2


how can I get all the keys with a liquid?

The expected result would be

param1,param2,param3


Any idea?


  • very ugly solution: {% for %} on the collection and concat the keys...

  • acceptable alternative solution: create a liquid filter "keys" ... sound too big that it does not actually exists ...


Answer

I suggest you an other format for your yml file

- id: param1
  key1: value1
  key2: value2

- id: param2
  key1: value1
  key2: value2

- id: param3
  key1: value1
  key2: value2

Then you could use {{ site.data.file | map: "id" | join: "," }}