# Generating pages programmatically

**URL:** <https://discourse.gohugo.io/t/generating-pages-programmatically/25900>\
**Category:** support\
**Created:** [May 29, 2020, 8:13am UTC](https://discourse.gohugo.io/t/generating-pages-programmatically/25900 "2020-05-29T08:13:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcel\_Overdijk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcel_overdijk/32/4821_2.png) [@Marcel\_Overdijk](https://discourse.gohugo.io/u/Marcel_Overdijk)\
**Post date:** [May 29, 2020, 8:13am UTC](https://discourse.gohugo.io/t/generating-pages-programmatically/25900/1 "2020-05-29T08:13:58Z")

</div>

Is it possible to generate pages programmatically with HUGO?

E.g. using a [Generator plugin](https://jekyllrb.com/docs/plugins/generators/) with Jekyll or using the [createPages](https://www.gatsbyjs.org/tutorial/part-seven/) 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.

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [May 29, 2020, 8:18am UTC](https://discourse.gohugo.io/t/generating-pages-programmatically/25900/2 "2020-05-29T08:18:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Marcel\_Overdijk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcel_overdijk/32/4821_2.png) [@Marcel\_Overdijk](https://discourse.gohugo.io/u/Marcel_Overdijk)\
**Post date:** [May 29, 2020, 8:23am UTC](https://discourse.gohugo.io/t/generating-pages-programmatically/25900/3 "2020-05-29T08:23:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [May 29, 2020, 8:42am UTC](https://discourse.gohugo.io/t/generating-pages-programmatically/25900/4 "2020-05-29T08:42:55Z")

</div>

> [@Marcel\_Overdijk](#):
>
> 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).

---

<div class="post-metadata">

**Author:** ![Marcel\_Overdijk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcel_overdijk/32/4821_2.png) [@Marcel\_Overdijk](https://discourse.gohugo.io/u/Marcel_Overdijk)\
**Post date:** [May 29, 2020, 9:50am UTC](https://discourse.gohugo.io/t/generating-pages-programmatically/25900/5 "2020-05-29T09:50:46Z")

</div>

Thx!

For reference I found this one: [https://github.com/gohugoio/hugo/issues/6310](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:

```auto
{{ $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.

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [May 29, 2020, 9:56am UTC](https://discourse.gohugo.io/t/generating-pages-programmatically/25900/6 "2020-05-29T09:56:11Z")

</div>

> [@Marcel\_Overdijk](#):
>
> For now it’s probably the best case to generate the pages in a custom script into the “sources” folder. And maybe .gitignore 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`.

---

<div class="post-metadata">

**Author:** ![Marcel\_Overdijk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcel_overdijk/32/4821_2.png) [@Marcel\_Overdijk](https://discourse.gohugo.io/u/Marcel_Overdijk)\
**Post date:** [May 29, 2020, 9:57am UTC](https://discourse.gohugo.io/t/generating-pages-programmatically/25900/7 "2020-05-29T09:57:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [May 29, 2020, 10:00am UTC](https://discourse.gohugo.io/t/generating-pages-programmatically/25900/8 "2020-05-29T10:00:07Z")

</div>

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

So that your life is easier.

---

<div class="post-metadata">

**Author:** ![klakegg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/klakegg/32/6373_2.png) [@klakegg](https://discourse.gohugo.io/u/klakegg)\
**Post date:** [May 29, 2020, 1:57pm UTC](https://discourse.gohugo.io/t/generating-pages-programmatically/25900/9 "2020-05-29T13:57:49Z")

</div>

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.
