Generating pages programmatically

Is it possible to generate pages programmatically with HUGO?

E.g. using a Generator plugin with Jekyll or using the createPages API with Gatsby.

My use case is that I have data in the HUGO data folder for which I would like to generate pages for.

Currently there isn’t a Hugo way to do what you ask, but as I understand your description above this can be done with a shell script.

Yes, I also generate the data files, so I could also generate pages.
But I would prefer to have those pages pages only in the build site in public not in the “sources” so they won’t end up in git.

No. This cannot be done, even with a shell script.

There are a couple of issues on GitHub about generating pages from data. I suggest you follow them.

(The links to the GitHub issues have been also posted in this forum, but right now I am on the go).

Thx!

For reference I found this one: https://github.com/gohugoio/hugo/issues/6310
I don’t know what the status is or if it will help me.

Probably better to explain my use case a bit more and @bep could maybe chime in.

I have data like:

  • data/continents/eu.yml
  • data/continents/na.yml
  • … more continents …
  • data/countries/uk.yml
  • data/countries/de.yml
  • … more countries …
  • data/car-manufactures/audi.yml
  • data/car-manufactures/bmw.yml
  • data/car-manufactures/cooper.yml

These yml’s are basically created from a database. Note the content in these yml’s is unaware of HUGO, it are simply key/values describing the data.

Now I use this data in other pages as well.
E.g. to retrieve a name, alpha2 code or alpha 3 code for countries like:

{{ $country := index .Site.Data.countries $country_id }}
{{ $country.name }}

But for all “records” in the car-manufacturers collection I would like to generate a dedicated page (so not for countries or continents).

Hope this makes it a bit clear.
For now it’s probably the best case to generate the pages in a custom script into the “sources” folder. And maybe .igitignore them.

At some point the Pages from Data feature will be implemented, but right now the above looks like the way to go. Also I suppose that by “sources” folder you are referring to the contentDir.

Yes I believe so to. And yes with “sources” I meant the content dir.

If you give those generated pages a prefix you could use .gitignore with a wildcard pattern.

So that your life is easier.

Using a script to generate the pages into content is currently the easiest way. However remember the frontmatter also accept JSON in addition to TOML and YAML, which makes it easier to automatically create content.