首页 文章

使用include标签访问Jekyll的_data文件夹中的csv

提问于
浏览
1

我正在努力简化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 %}

帖子中的片段:

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

如何为FOLDER1使用变量,以便我可以将它用于多个帖子?

1 回答

  • 1

    如果要将包含重用于多个数据源,可以使用括号表示法,如下所示:

    {%- 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 %}
    

相关问题