[solved] ".Site.GetPage" function not working with "_index.md"

Should the .Site.GetPage function work with _index.md content?

Using the following directory structure:

/content
/content/post
/content/post/hello.md
/content/_index.md
/content/world.md

{{ with .Site.GetPage "home" }}{{ .Title }}{{ end }} returns an empty result (incorrect) while {{ with .Site.GetPage "page" "_index.md"}}{{ .Title }}{{ end }} generates an error.

Many thanks in advance.

Your first construct is correct. Note that there is a shortcut in {{ with .Site.Home }}

As to why you get an empty .Title, that is hard to tell from the little you show us (is there a title in the _index.md file?). Do you have a link to the source?

Hi Bjørn, thanks for the swift response and confirmation.

The source is not public but the content of _index.md is very simple:

---
title: "Welcome"
date: 2017-07-07T14:44:40+01:00
draft: false
---

As an aside using {{ (.Site.GetPage "home").Date }} returns 0001-01-01 00:00:00 +0000 UTC rather than the date specified within _index.md, perhaps the file is being ignored?

For reference this has been tested on Ubuntu 16.04 LTS with Hugo 0.31 (http://localhost:1313).

You should maybe look for an error/warning in the console (run with -v) … but that is the limit of the support your getting without the source.

Appreciate the update though executing hugo server -v doesn’t report any relevant error/warning.

Unfortunately the issue doesn’t occur when creating a simple example site so we’ll try and locate the cause systematically and report here for completeness.

Many thanks again.

After further testing it appears the issue was caused by an empty taxonomy value within config.toml, despite no error or warning being generated (hugo server -v):

[taxonomies]
  author = ""
  category = "categories"
  tag = "tags"

Is this a known limitation/issue?

Many thanks as always.

No. Don’t know how this was the culprit for your problem.

I just tested what you wrote above because I regularly use empty taxonomies in my projects. The reason being I take advantage of Hugo’s taxonomies for internal purposes without exposing unnecessary URLs.

And I am able to get the homepage title in permalink pages by calling it in the single pages template at /layouts/_default/single.html while having an empty taxonomy in my config.

In which template have you tried to get the title and failed?

We’ve created a simple test site using the minimo theme:

https://ufile.io/ebjnc

Line 12-16 of themes/minimo/layouts/partials/nav/menus/main.html was customized to highlight the problem at the top of every page, commenting-out author within config.toml resolves the issue and should successfully display the title of _index.md.

Hopefully this will help in diagnosing the issue.

This looks more like a Minimo theme bug than a Hugo bug. Since I cannot reproduce it with a custom theme that I’m using.

It does happen in your project of course. And I did try placing the snippet in different parts. It fails every time unless of course the empty taxonomy is either removed or filled in.

This shouldn’t be the case.

Maybe you should raise this issue with the Minimo theme creator @MunifTanjim or try a different theme.

Agreed, the problem also happens on our own custom theme but minimo was used in order to provide a public example for diagnosis.

This still appears to be a Hugo bug/limitation but perhaps something in your theme’s config.toml is either masking or overriding the issue?

No. My config pretty much has what you have. Except for uglyURLs = true Adding this didn’t replicate your issue. Also note that currently I’m on the latest Hugo 0.31.

Try a different theme and see what happens. My theme is a modified version of the Cactus theme

Aha! I finally figured out what’s going on. And it’s not a Minimo theme issue.

Front matter from _index.md is not picked up when a taxonomy is left blank in config.toml

In my project I didn’t use an _index.md and adding one I was able to replicate your issue.

Turns out that you were spot on from the start @mimattr

So as a second fix, if you really need a blank taxonomy just don’t use an _index.md for your homepage. Don’t think you really need one anyway. You can simply replicate it’s front matter as parameters in your config.toml and then call them in your index.html template.

But this issue is probably something for @bep to look at. Since it looks like a Hugo bug.

1 Like

Appreciate the confirmation and continuing to look into this issue @alexandros

Unfortunately the _index.md feature is important to our theme so we will simply comment-out any empty taxonomy values within config.toml for now.

Hopefully @bep has some ideas regarding an official fix.

Many thanks again.

Your setup is ambiguous and not supported by Hugo. It never will. We should probably have logged an ERROR in this case, but that will not help you.

Can you elaborate on which parts of the setup are ambiguous regarding the source code provided? It was only created as a simple example though has already proven to affect 2 separate sites.

If this is a limitation Hugo should definitely log an ERROR/WARN, silent failures are not particularly useful for anyone. :wink:

Many thanks in advance.

Home page vs the empty taxonomy term resolves to the same place on the file system. You could try to disable the taxonomyTerm kind. We have an issue to handle this on the render side, but someone will have to get to it.

Thanks for the clarification, we’ll simple comment-out empty taxonomy terms within config.toml for now, hopefully this issue is resolved in a future release.

Many thanks again.