DEV Community

Cover image for Sharing my "Year in Books" using the Zola static site generator `load_data` method
Jared Forth
Jared Forth

Posted on

Sharing my "Year in Books" using the Zola static site generator `load_data` method

I recently migrated my personal website from HUGO to the Zola static site generator. It's been incredible to use, and I wanted to share a real life use case of one of the more obscure features - load_data

Something I started doing a few years back is sharing my "Year in Books" in the /words section of my site. This was trivial with HUGO, but it took a bit of digging through the Zola docs to figure out how to accomplish it in the new system. It was also simple to do in Zola, but thought I'd share my approach.

I use Goodreads to keep track of what I'm reading, so the first step is to export a CSV file with my data.

The next step is to simply load that data and render what is desired. I want to be able to use a shortcode, so I created templates/shortcodes/books.html to be used like:

{{ books(year="2024")}}
Enter fullscreen mode Exit fullscreen mode

in a markdown file.

The snippet itself is:

{% set data = load_data(path="/content/books.csv") -%}

<table>
  <thead>
    <th>Title</th>
    <th>Author</th>
    <th>Date Read</th>
  </thead>
  <tbody>
    {% for record in data.records %} {% if year in record[14] %}
    <tr>
      <td>{{ record[1] }}</td>
      <td>{{ record[2] }}</td>
      <td>{{ record[14] }}</td>
    </tr>
    {% endif %} {% endfor %}
  </tbody>
</table>
Enter fullscreen mode Exit fullscreen mode

which creates a table like:

Image description


It ended up taking me only about 15 minutes to implement this feature, which speaks to how user-friendly the Zola static site generator is. Hope this is helpful for someone looking to import data into their static site.

Top comments (0)