So you’re using the Hugo static site generator and interested in auto-generating pages from data, similar to Gatsby’s programmatic generation or Middleman’s Dynamic Pages ? As of late 2021, Hugo does not have built-in support, and since the topic has been in discussion since at least 2013 , don’t hold your breath. Luckily, there’s options available to accomplish the same task, from premade tools to writing your own simple scripts.
While Hugo itself may not have support for creating pages from a data source, as long as you generate files in locations it expects, you end up with the same outcome. The following approaches all follow the process of taking input data (often in JSON or YAML), generating Markdown files based off it, and placing them into the content/
folder. Though it’s the most common case, it’s not only for Markdown, the same process could be used for HTML template files, or any other file Hugo recognizes.
A Node.js project which handles the content generation process, all you need to do is install it’s dependencies and you’re ready to go. I would have used this option if I had found it before I created my custom script.
For easier content management than a JSON file, explore the many CMS’s available in the wild. Check if they have the capability to connect with your website hosting provider’s build process, then it can generate pages from your data each time it builds the site. One combination with an extensive tutorial is Hugo + Sanity.io + Netlify .
If you want direct control of exactly what’s happening with your files, you can write your own custom script in whatever language you choose. I personally chose to use a Python script + a YAML data file for my first version, but unfortunately pyyaml
isn’t in the standard library. If you don’t want to bother with managing Python dependencies (I’m in that same boat), there is an improved script below that uses only JSON
for the configuration files.
Whether the config is handled through YAML, JSON, or whatever flavor of the month, I use a simple Makefile
to handle the process: generating new pages, cleaning out existing content, and putting the fresh set in.
For the script using YAML, an abridged version of tools.py
looks like:
Another version I’ve written uses JSON and moves the templates out of the Python script and into their own file:
And the corresponding template file tmpl.md
:
And corresponding data file examples.json:
You should now have a enough of an understanding of the current landscape to make the best choice for your circumstances. Despite Hugo not offering built-in support, there’s plenty of alternatives to programmatically generate pages. Whether you decide to write your own or use existing tools, there’s no bad option, choosing any of them will save you the time and frustration of manual creation.
A simple guide to self-host n8n in just a few minutes on fly.io.
A simple guide to self-host an Actual server in just a few minutes, for free.
A simple guide to self-host a NocodDB Airtable alternative in just a few minutes, for free.
Let the nice robots at GitHub handle sending Venmo requests to your friends.
Plugins to make Obsidian as comfortable as other note apps, aimed at those who want tools that don’t need tinkering.