使用包含标签访问 Jekyll 数据文件夹中的 csv

Acessing csv in _data folder of Jekyll using an include tag

我正在尝试简化 Jekyll 网站的数据组织和使用。我正在尝试访问 CSV 中的单个行或值,使用此代码段成功:

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

数据位于_data文件夹下的文件夹中,如下:

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

按命名列访问 CSV 的包含:

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

post中的片段:

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

如何为 FOLDER1 使用变量,以便我可以将其用于多个 post?

如果你想为多个数据源重用你的包含,你可以像这样使用括号表示法:

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

或者,如果您有像 data_source: FOLDER1

这样的页面变量
{%- include speclocator.html name="Thing" data_source=page.data_source -%}

在您的包含中:

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