创建 CSV 数据文件

特点说明

  • 生成后的 HTML 表格,行、列的顺序和 CSV 文件中一致
  • 也可以使用其他格式数据文件实现,比如 JSON 或 YAML
  • 能够拆分表格的标题行

创建数据文件

在数据文件目录创建一个 CSV 数据文件,例如 authors.csv,路径和内容如下:

路径:

_data/test/authors.csv

内容:

First name,Last name,Age,Location
John,Doe,35,United States
Jane,Doe,29,France
Jack,Hill,25,Australia

在模板或者页面调用 CSV 数据并生成 HTML 页面

两个文件都创建好后,可以构建,通过 /test/table_test/ 目录查看效果

---
layout: 
title: Table test
permalink: /test/table_test/
---

<table>
  {% for row in site.data.test.authors %}
    {% if forloop.first %}
    <tr>
      {% for pair in row %}
        <th>{{ pair[0] }}</th>
      {% endfor %}
    </tr>
    {% endif %}

    {% tablerow pair in row %}
      {{ pair[1] }}
    {% endtablerow %}
  {% endfor %}
</table>

回到顶部

Copyright © 2017-2024 1px.run (像素教程) Distributed by an MIT license.

Site updated at 2024-04-23 12:45