Single template lookup revisited

Hello,

I’ve spent much uninterrupted time with

but something seemingly extremely simple continues to elude me. Here’s the location of my contact-page content:

content/contact/index.md

“Contact” is a Leaf Bundle. It will never have child pages, so it doesn’t get an _index.md file, but rather an index.md file.

And here’s the location of the layout i expect to render for the Contact page:

layouts/contact/single.html

The layout, however, is ignored. Instead, the layout in layouts/_default/single.html is rendered.

If, however, i make these changes:

  • content/contact/index.mdcontent/contact/_index.md
  • layouts/contact/single.htmllayouts/contact/section.html

then the template renders (with no changes to the front matter).

But, “Contact” is a Leaf Bundle, not a Branch Bundle. I don’t want to make it a Branch Bundle. I have this same problem with every other Leaf Bundle page on my site (e.g. content/about/index.md, content/support/index.md, etc.).

Based on the documentation, it doesn’t seem that i should not need to declare kind, type, or layout in my front matter, given the architecture i’ve outlined above. It seems to me the default lookup order should render the single.html template, just as it does section.html templates.

What am i missing?

Try creating your new single template at:

layouts/page/single.html

OR

In your contact page front matter, set

---
type: contact
---

Then create your single template at:

layouts/contact/single.html

Thanks, @zwbetz, type: contact does render the template. I wouldn’t want to use layouts/page/single.html since my site has a quite a number of different, page-specific layouts.

Maybe i’m just being stubborn. My assumption was that i wouldn’t need to specify anything specific in my front matter, given Hugo’s default lookup order. Given the architecture i described in my original post, it seems to me that it should work without the type specification. I’d still like to know what i’m missing. But, in the meantime, your feedback keeps me moving forward. So thanks again.

Perhaps there is a way to do what you want, I just don’t know how. Maybe others can help you.

Perhaps this helps:

https://discourse.gohugo.io/t/my-experiences-with-hugos-template-lookup-order/9959/3
https://discourse.gohugo.io/t/see-which-template-is-used-by-hugo/9983/3

Thanks @Leo_Merkel. I think the essence of my question is reflected in @Michael_Schreiber’s comment in the first link you provided:

Why doesn’t content templating work under the THEME directory structure?

For now, i’m sticking with declaring type in my front matter, which allows me to mirror my content and layouts directories seemingly as closely as possible.

I agree with your assessment, one should be able to catch any content based on lookup order. If that is not the case, I would like to know how it works. Here’s my deal, I have a hard time conceptualizing these, so I need to get in and mess with templates and see what happens.

So… do you happen to have a test repo with the templates and content files in place? Otherwise I’ll have to go do that, and I don’t quite have time for that today. Let me know! :slight_smile:

Thanks @maiki, i decided it was best to shoot myself in the foot and make my first Hugo project extremely complex. Virtually every support post i’ve made to date comes as a result of a client project i’ve been working on for way too long. To your point, in the next week or so, i will have a repo i’ll be happy to share.

Sidebar on the topic of complexity: i found my way to Hugo after i was burned one too many times by Drupal. Of course i’m still learning, but, so far, i’m not wanting for functionality—which is really impressive!

1 Like