Pass Frontmatter to Taxonomy Without _index.md

Hugo Taxonomies are great but just like everything else in life, they currently have one major flaw (at least for me).

When the user needs to pass content to a taxonomy list page she must use the following path /taxonomy-name/taxonomy-term-_index.md and repeat this per taxonomy term.

Obviously that is a lot of folders to maintain not only when organizing a Hugo Site but especially when the content of taxonomy list pages needs to be updated.

There has been some discussion about overriding this limitation during the past few days with the introduction of new and a kind of hacky technique that was developed by @guayom , a Github Issue by yours truly (that ended up in a misunderstanding) and a blueprint for the Hugo taxonomy generator (that does not really address the folder problem) by @rdwatters

This is my take on this issue. It works 100%. It’s fast. And I’m happy.

It is a very simple technique that utilizes front matter parameters (in this example just plain old tags) and Hugo Sections. And I can’t see this workaround breaking in the future unless of course Hugo changes beyond recognition. Or of course I’m missing something. Anyway enough talk.

Step 1

Assign tags to your posts as you usually do like:

tags = [ "handbags", "versace" ]

Step 2

Under /content/ create a folder called brands

Within this folder create .md files for as many taxonomy terms as you need.

In your front matter include the title and description parameters you want to have rendered on your taxonomy list page, as well as a very important parameter that needs to contain the exact same tag you assigned to your posts in Step 1.

You can call this new parameter anything you want in my example I’ve called it folksy (from Folksonomy) (thanks for the link @rdwatters ) :

+++
title = "Versace AW 2017"
description = "La Nuova Collezione di Borse Versace"
folksy = "versace"
+++

Step 3

Now in the template for our custom brands taxonomy (that is really just a plain old Hugo Section) located at /layouts/brands/single.html place the following:

 {{ $tag:= .Params.Folksy }}
 {{ range ($.Site.GetPage "taxonomy" "tags" $tag).Pages }}
   HTML HERE
 {{ end }}  

First I assigned a variable for the folksy parameter

{{ $tag:= .Params.Folksy }}

And then I used range with a nested .GetPage function to fetch the posts I need while using the above variable to fetch the tag dynamically (sneaky ain’t it?).

{{ range ($.Site.GetPage "taxonomy" "tags" $tag).Pages }}

And that’s it!

Now you have just one taxonomy folder with the full taxonomy terms list pages and you can update them as often you need without clicking your life away.

TIP: You can also use this technique to render a taxonomy terms menu anywhere you need on your site and pass meta descriptions and other attributes to the <head> of your Hugo Project.


I would like to thank @spf13 @bep and all the other developers for their time and hard work in making the greatest static site generator that even me a non-Dev Art Director can understand.

Special thanks to @moorereason @rdwatters @jura @budparr and all the others who have helped me in this forum while I was here.

Keep Hugo-ing!

I’m out of here for now. But I’ll be around…

2 Likes

This is cool, is a combination between both techniques. I’m definitely going to use your technique for the single.html template.

Now, I also like to avoid the use of /brand/_index.md … although I didn’t use it in my post (Handling taxonomies like a boss - update), I’m using it on a real project ( :zipper_mouth_face:). I can’t see how doing this would break something. I’ve done a lot of testing and haven’t found any problems yet. But since it’s considered a bug and needs some reviewing from the devs, I documented it in the most “official” way possible, hoping that maybe at some point, it can be added to the documentation, to help other users.

So, thanks for you ideas! My project is better now, thanks to your help.

I did not try to follow these steps yet, sorry…
But: I don’t see the brand folder reused in the steps following Step 2. Is there something magic I miss or should the folder be tags, if the line ranging through the tags is

{{ range ($.Site.GetPage "taxonomy" "tags" $tag).Pages }}

Also, does this still work today or is it more or less outdated by another great feature in the meantime? My english vocabulary does not seem to help me searching for this issue on this community. I try to add content to taxonomy list pages (pages with all posts per tag) like photos, links and description, but not for all tags, just some.

This is a very old tips and tricks.

I moved away from this technique a while ago.

Also note that .GetPage was re-written last June (I think) and it was a breaking change. So the code here is outdated.

What are you trying to do?

I am trying to add content to some (not all) taxonomy term pages (those pages that list articles of the same tag).

I found a way via /tags/TAGNAME/_index.md and now try to adapt my templates to check if metadata is available. I think that should do the trick.

Somewhere I read there is a “create pages from data files” in the works… probably a good point to look at making this easier again.

Here is how I enter front matter in taxonomy pages these days without the use of _index.md

I create a partial and call it from within the <head> tag of my templates.

<title>{{ partial "title.html" . }}</title>

Then this partial contains a long list with the following:

{{ if in (.Permalink | string) "/some-taxonomy-URL/" }}<---- custom meta ---->{{ end }}

I much prefer having one file with a long list of page titles rather than several folders that contain various _index.md. At least for me it’s easier to maintain.

Also this technique can be used for creating layouts in any Hugo section or taxonomy you need from within the /_default/layouts/list.html/

I’m well aware of the Hugo’s template lookup order but I much prefer to have one template to rule all lists, rather than having several templates in different directories.

Also this technique is fast. Meaning that I haven’t noticed any differences between organizing the templates as is recommended by the Hugo Docs and the way that I just mentioned above.

2 Likes