How to associate / connect between content files (md) and data files

I am working on a personal portfolio website which includes some project pages for films I worked on. They should all have a “credits” section. I want this section across all “movie” pages to have the same style and follow the same structure.

The way I was thinking of approaching this is to have YAML files in the data folder, each with the same file name as the corresponding .md file. Then I would use some function to automatically have the template look for the data source with the same name.

So for example I have movie1.md which would somehow look automatically for a data file under DATA/CREDITS called movie1.yaml

So my questions are:

How would I make this connection inside the template?

Is this even the right approach? I imagine this isn’t an uncommon thing so I assume there’s a ‘right way’ of doing this.

Lastly how would you go about creating a toggle, so that if there is no associated file - it simply isn’t creating that section on the page?

Just for context: I am a non-professional web developer, coming from film/design/animation background. I have some background in HTML and recently learned some CSS. I’m doing this as a personal challenge (And also to get rid of my Wix account), so I have better understanding than a total non-developer, but definitely a lot of holes in my knowledge compared to someone who does it for a living.

Thanks

I think of the /data data shared by many.

If you have data used by one (or e.g. a section) pages, I would recommend looking into page bundles, e.g. YAML files that you load with

This is also much more efficient for “big data” (because Hugo can free the memory when it’s not in use anymore).

This way you don’t need to know much about where it’s located, you can do e.g.:slight_smile:

{{ $movie1 := .Resources.Get "movie1.yml" }}
{{ $firstMovie := .Resources.GetMatch "movie*.yml" }}
{{ $movies := .Resources.Match "movie*.yml" }}
{{ $moviesInHomePage := site.Honme.Resources.Match "movie*.yml" }}
{{ $moviesInAssetsRoot := resources.Match "movie.*.yml" }}
{{ $moviesInAssets := resources.Match "**/movie*.yml" }}

And variants of the above.

Thanks for the quick reply!

So you’re saying if the data is only relevant to a specific page, keep it as a data file in that page bundle instead of referencing a data file in the data folder?

I tried this a few months ago and Hugo server would often fail to reflect the changes in the watcher. Moving the files to the assets folder and the issue has never recurred since.

You could also just store the credits in the frontmatter of movie1.md

---
title: Movie 1
params:
  credits:
    director: foo
    producer: bar 
    executive_producer: baz
---

Then in the page template, grab the page params…

<ul>
  {{ with .Params.credits.director }}<li>Director: {{ . }}</li>{{ end }}
  {{ with .Params.credits.producer }}<li>Producer: {{ . }}</li>{{ end }}
  ...

This would keep the movie content and credits all in the same file.

Yup, that’s also a good option. My initial thought was that it would make the front-matter a bit busy, but it would also make things a bit simpler.