Skip to content

Latest commit

 

History

History
87 lines (64 loc) · 2.32 KB

README.textile

File metadata and controls

87 lines (64 loc) · 2.32 KB

Jekyll CSV Data Reader Plugin

This plugin makes data in csv tables available in Jekyll, similar to what Jekyll already does with YAML files.

Installation

Download read_csv.rb and put it in the _plugins directory of your website.

Usage

This plugin reads all csv files found in the _csv directory and, for each file <>.csv it makes the following information available for use with Liquid:

  • site.data.<<name>>.rows
  • site.data.<<name>>.cols
  • site.data.<<name>>.keys
  • site.data.<<name>>.content
  • site.data.<<name>>.content_hash

where:

  • rows and cols contain, respectively, the number of rows (without headers) and the number of columns of the table
  • keys is an array containing the header of the table
  • content is an array of arrays containing the table data
  • content_hash is an array of hashes, to access data using the columns names set in the header.

Example

Suppose you have an example.csv file stored in the _csv directory of your Jekyll website, with the following content:

 h1,h2,h3
 a,b,c
 1,2,3

Then, the following Jekyll template generates an HTML representation of the table:

---
title: Example
---
<table>
  <thead>
    <tr>
    {% for header in site.data.example.keys %}
      <td>{{header}}</td>
    {% endfor %}
    </tr>
  </thead>
  <tbody>
    {% for row in site.data.example.content %}
    <tr>
    {% for column in row %}
      <td>{{column}}</td>
    {% endfor %}
    </tr>
    {% endfor %}
  </tbody>
</table>

If you prefer to access cells using header names, you can use the content_hash key. For instance, the following snippet extracts the content of column h1 and h3 from the table:

{% for row in site.data.example.content_hash %}
<tr>
  <td>{{row.h1}}</td>
  <td>{{row.h3}}</td>
</tr>
{% endfor %}

See the example directory for more details.

Alternatives

Store the table data you want to access in _data, as a YAML file.

Known Bugs

The plugin does not check for name clashes of files stored _data and _csv directory. Do not use the same name for files in _data and _csv.

In other words, if you have a _data/w.yaml file, do not create a _csv/w.csv file.

License

Distributed under the terms of the MIT License