Is there a way to access the column names in the header row of CSV data?

See https://github.com/jekyll/jekyll/issues/8086

Hi @chmaynard

Yes you can do it with a for loop or with a custom plugin.

Sample data

Copied from https://jekyllrb.com/docs/datafiles/

_data/members.csv

name,github
Eric Mill,konklone
Parker Moore,parkr
Liu Fengyun,liufengyun

For loop approach

You can’t store the data like this but you can represent it.

Here is the first row.

{{ site.data.members[0] }}

So iterate with a for loop. Within the loop, item[0] will give key and item[1] will give value.

<ul>
{% for item in site.data.members[0]  %}
  <li>
      {{ item[0] }}
  </li>
{% endfor %}
</ul>

Result:

  • name
  • github

You could tweak the HTML to make the results all appear on one line or as a table header.

Plugin approach

If you want to take the keys and store them as an array, there is no .keys builtin. However, there is an existing plugin on Github which works great. https://github.com/dalenys/jekyll-keys-filter

I copied the Ruby file to my _plugins directory as keys-filter.rb

module Jekyll
  module KeysFilter
    def keys(input)
      input.keys
    end
  end
end

Liquid::Template.register_filter(Jekyll::KeysFilter)

Then I ran the keys filter against the header row.

{{ site.data.members[0] | keys | jsonify }}

Result:

[“name”,”github”]

Joining values with a comma.

{{ site.data.members[0] | keys | join: ', ' }}

Result:

name, github

@MichaelCurrin Very interesting solution.
Would you be willing to write a small tutorial for the Jekyll documentation site…?