How to create relationships on a one-to-many basis

Hugo 0.17.

Summary: I’m want to create relationships between content where front matter values match the slug of the content to be related. For example, returning the bio of an author in the “authors” section on an article page in the “articles” section. I’d like to do this with single variables or an array.

Given two pieces of content:

  1. Articles

which have set in the front matter, a parameter

author: author-name
  1. Authors

which have content (assuming that “author-name” is the slug generated for this):

title: Author Name
Bio content here, for example.

I know finding the author by its slug (File.BaseFileName) works and I can return the author’s bio on the article page:

{{if isset .Params "author" }}
  {{ range where .Site.Pages ".File.BaseFileName"  }}
    {{ .Content }}
  {{ end }}
{{ end }}

However, if some (perhaps not all) of my articles have an array in the front matter:

  - author-name
  - author2-name

I can’t seem to return anything. I’ve tried using “in”

{{ range where .Site.Pages ".File.BaseFileName" "in"  }}

but that’s too broad.

And “intersect”

{{ range where .Site.Pages ".File.BaseFileName" "intersect"  }}

which is what I would think would be the correct method, doesn’t appear to work.

Pointers for the noob?

Actually, I think this works:

I thought it was too broad because I happened to have a file named with a slug a that I was unaware of that made me think it was pulling something like a sub-string, but it doesn’t appear to be.

Would love, though, if any more experienced Hugo folks have an opinion on this approach, to chime in.

@budparr Have you considered using taxonomies as well? With V18, everything is a page, so you can add content to individual taxonomy terms…

I don’t have .18 yet (I use brew), but I’ll check it out. Just finding my way around, as you know. However, the example i used is just that. My actual use-case is one where the things I’m creating relationships between are sections of legitimate content themselves.


Hey, @rdwatters - just thinking about this. Re-reading your comment and thinking about this. Is there anything in particular that I gain by having some content designated as a taxonomy? That is, more flexible access to it, I suppose.

Also, note, that this content will have taxonomies themselves (my authors are actually “contributors” and will also be broken down into subtypes, which I would use taxonomies for, so I may, to complicate the above, be trying to look up them up not just by relationship, but by taxonomy as well - what I’ve seen so far leads me to believe I’ll be able to do that; fingers crossed)

@budparr Just reading this. In TX visiting family, but you pose some interesting ideas—specifically the idea of a taxonomy within an for a taxonomy/term page. I’ll post a (likely painfully thorough) response in the next couple days. I’m still working through the recent changes bep has added to 18.

1 Like

Just following up here for anyone who comes along. My original answer to myself did work. The “in” included the array I was creating in my content. Here’s a complete example where I’ve also narrowed the scope of the where statement to just look in the “author” content:

{{ if .Params.authors }}
  {{ range first 2 (where (where .Site.Pages.ByDate.Reverse "Section" "author") ".File.BaseFileName" "in" .Params.authors) }}
      {{ .Content }} 
  {{ end }}
{{ end }}

Interestingly. @rdwatters, taxonomies didn’t work in this case, because my situation is a bit too complex.

In my authors section, one author may take on many roles: In an article they may be listed separately as an author and an editor, so, because taxonomies are created by their use in content (i.e. when they are linked to in the front matter of a document), the author would show up with multiple URLs. For example, an author who is linked to as “author” and a “editor” would show up on the site twice “/author/author-name”, and “/editor/author-name.”

At any rate, I was still able to accomplish what I wanted without declaring them taxonomies, and your prompt got me to dig into taxonomies some more. Still getting used to things. Thanks!

1 Like