New template lookup order also with _partials possible?

Hi, I was thinking this update (0.146) was just the right time to simplify the structure of my hugo site and reduce redundant templates.
I got the feeling I had understood the new simpler lookup order but it turns out in practice some things are not quite working as I imagine. (It didn’t help that ChatGPT gave the typical very confident but false answers, either :wink:

So what I’m asking is: shouldn’t the partials also follow some lookup logic akin to layouts?

< Template types > is somehow not working for me:

{{ partial "footer.section.de.html" . }}

Hugo uses this lookup order to find a matching template:

  1. layouts/_partials/footer.section.de.html
  2. layouts/_partials/footer.section.html
  3. layouts/_partials/footer.de.html
  4. layouts/_partials/footer.html

I have a “splash-type” for some of my pages:

layouts/splash/home.html
layouts/_partials/featured.splash.html
index.md contains type: "splash"
splash/home.html contains {{ partial "featured" . }}

hugo correctly selects home.html from /splash but uses /partials/featured.html instead of featured.splash.html which I thought the above quote was saying.

I also tried to be more specific with {{ partial "featured.splash" . }} and {{ partial "featured.splash.html" . }} but no change… and at that point I changed it back to featured-splash.html which defeats the whole point.

What am I missing?

Unlike other template types, partial template selection is based on the file name passed in the partial call. Hugo does not consider the current page kind, content type, logical path, language, or output format when searching for a matching partial template.

guess the problem here is (regardless of the type)

never tried before but for me the docs page tells me:

  • having these templates in

    foo.bar.html --> <h4>Foo Bar</h4>
    foo.html     --> <h4>Foo</h4>
    
  • you will get:

    {{ partial "foo.bar.html" }}  --> Foo Bar
    {{ partial "foo.html" }}      --> Foo
    {{ partial "foo.oops.html" }} --> Foo
    

in 0.147.9 I get: Foo for all three

0.145.0 I get as expected

  • Foo Bar
  • Foo

but the last one raises errors with error calling partial: partial "foo.oops.html" not found


p.s. the examples use section and de in the name which may make one think it is related to these common template parts types/lang …

What’s “bar”?

:wink: just a part of the partial name

combining this:

Unlike other template types, partial template selection is based on the file name passed in the partial call. Hugo does not consider the current page kind, content type, logical path, language, or output format when searching for a matching partial template.

and that from Template types # Partial

{{ partial "footer.section.de.html" . }}
Hugo uses this lookup order to find a matching template:

  1. layouts/_partials/footer.section.de.html
  2. layouts/_partials/footer.section.html
  3. layouts/_partials/footer.de.html
  4. layouts/_partials/footer.html

might be the the intention is it only works for lang and kind ?

So what happens when “foo” and “bar” are valid output formats?

I just want to say, regardless the technical feasibility, if the logic from template types#partial would work with “types” (and not just .section and .de like mentioned there) this would minimize some of the lower level templates (i.e. home, single,…) and even some other partials (you can call partials out of partials, right?) would be redundant in my projects.
But I’m still just beginning to grasp the full power of hugo.

does not consider … output format

so I guess nothing, because of

partial template selection is based on the file name passed

as I said, I never tried this way of selecting some partials… just folder structure - and at least to me the docs here are not as precise as on other places …

wait. I actually never tried this in my case… would this work with partials?

NO.

Example 1

Given the example lookup order in the docs:

  1. layouts/_partials/footer.section.de.html
  2. layouts/_partials/footer.section.html
  3. layouts/_partials/footer.de.html
  4. layouts/_partials/footer.html

And this call:

{{ partial "footer.section.de.html" . }}

If the de language has not been defined, Hugo ignores the language designator, and layouts/_partials/footer.section.html will be selected.

Example 2

Given this _partials directory:

layouts/_partials/
├── foo.bar.html
├── foo.html
└── foo.oops.html

And these calls:

{{ partial "foo.bar.html" }}
{{ partial "foo.html" }}
{{ partial "foo.oops.html" }}

If the bar and oops output formats have not been defined, Hugo ignores the output format designator, and layouts/_partials/foo.html will be used for all three.

I’m sorry you find this documentation insufficient:

Unlike other template types, partial template selection is based on the file name passed in the partial call. Hugo does not consider the current page kind, content type, logical path, language, or output format when searching for a matching partial template. However, Hugo does apply the same name matching logic it uses for other templates. This means it tries to find the most specific match first, then progressively looks for more general versions if the specific one isn’t found.

Perhaps someone with a better command of the English language can make a suggestion.

so definitely not me. I’ll read the whole page two more times with your explanations i may get it


partial template selection is based on the file name passed
Hugo does not consider the current …
find the most specific

lead me to partial filename is just a name and not contains semantic stuff like kind, output format…

starting with this

adding background from lookup order (standard template naming rules) and your explanation in this issue e.g. “output formats have not been defined” and ofc some forum topics…

…hope I got it and will play with

thx for your guidance, time and patience

sry for being unclear and missleading:

no, that’s just having /layouts/_partials/foo.html and /layouts/_partials/foo/bar.html for grouping. No fallback here.