Skip to content

Commit

Permalink
Update table.md
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelCurrin authored Sep 10, 2024
1 parent 5be593e commit af907d6
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cheatsheets/jekyll/templating/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Given CSV file:
{% for author in site.data.authors %}
<tr>
<td>{{ author['First name'] }}</td>
<td>{{ author['First name'] }}</td>
<td>{{ author['Last name'] }}</td>
<td>{{ author['Age'] }}</td>
<td>{{ author['Location'] }}</td>
</tr>
Expand All @@ -120,9 +120,10 @@ Given CSV file:

### Dynamic table

This approach is more flexible - it will work for any CSV and render all columns without having to reference them specifically.
Notes:

Data is unpacked as a tuple of keys and values for each row, so we name this `pair` and slice it. Also note use of dynamic header - we read the column names for the first row.
- Here using the `tablerow` builtin Liquid method to render a row. No need to add `tbody`, `tr`, or `td` elements for the rows. Note data is unpacked as a tuple of keys and values for each row, so we name this `pair` and slice it.
- By using a inner `for` loops for the header and data rows, this approach is more flexible - it will work for any CSV and render all columns without having to reference them specifically.

```liquid
{% assign rows = site.data.authors %}
Expand All @@ -144,4 +145,10 @@ Data is unpacked as a tuple of keys and values for each row, so we name this `pa
</table>
```

Since this is dynamic, you can easily move it to an `includes` file and then pass data to it and reference as:

```liquid
{% assign rows = includes.authors %}
```

{% endraw %}

0 comments on commit af907d6

Please sign in to comment.