1
votes

I am trying to simplify the organization and use of data for a Jekyll website. I am trying to access individual rows or values in the CSV, which is successful using this snippet:

{%- include speclocator.html name="Thing" -%}

Data is located in folders under the _data folder, as below:

_data/FOLDER1/specifications.csv, 
_data/FOLDER2/specifications.csv, 
_data/FOLDER3/specifications.csv,
etc.

The include to access the CSV by named column:

{%- for spec in site.data.FOLDER1.specifications -%}
{%- if spec.name == include.name -%}
{{ spec.value | strip }}
{% endif %}
{% endfor %}

The snippet in the post:

{%- for specification in site.data.FOLDER1.specifications -%}
<tr>
<td><strong>{{specification.name}}</strong></td>
<td>{{specification.value}}</td>
</tr>
{% endfor %}

How can I use a variable for FOLDER1, so that I can use this for multiple posts?

1

1 Answers

1
votes

If you want to reuse your includes for multiple data sources, you can use bracket notation like this :

{%- include speclocator.html name="Thing" data_source="FOLDER1" -%}

or, if you have a page variable like data_source: FOLDER1

{%- include speclocator.html name="Thing" data_source=page.data_source -%}

In your include :

{%- for spec in site.data[include.data_source]["specifications"] -%}
  {%- if spec.name == include.name -%}
    {{ spec.value | strip }}
  {% endif %}
{% endfor %}