Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ...
like image 655
nemenems Avatar asked Aug 12 '16 08:08

nemenems


2 Answers

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: "," }}

like image 166
Matt Avatar answered Nov 15 '22 19:11

Matt


Ok, I implemented a small filter to get the hash keys :

https://github.com/MichaelCurrin/jekyll-keys-filter

Just have to write something like:

{{ hash | keys }} 
like image 1
nemenems Avatar answered Nov 15 '22 20:11

nemenems