tpl: Fix some baseof lookup issues

We were mistakingly using the templates (e.g. list.html) descriptor to resolve the base template and not the page,
which worked fine in most cases, but not all.

Fixes #13583
This commit is contained in:
Bjørn Erik Pedersen 2025-04-11 10:16:28 +02:00
parent e3e3f9ae17
commit 9221cbca49
2 changed files with 33 additions and 5 deletions

View file

@ -166,3 +166,31 @@ p3.current.Ancestors.Reverse: {{ with templates.Current }}{{ range .Ancestors.Re
"p2.current.Ancestors: _partials/p1.html|all.html", "p2.current.Ancestors: _partials/p1.html|all.html",
) )
} }
func TestBaseOfIssue13583(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
-- content/_index.md --
---
title: "Home"
outputs: ["html", "amp"]
---
title: "Home"
-- layouts/baseof.html --
layouts/baseof.html
{{ block "main" . }}{{ end }}
-- layouts/baseof.amp.html --
layouts/baseof.amp.html
{{ block "main" . }}{{ end }}
-- layouts/home.html --
{{ define "main" }}
Home.
{{ end }}
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/index.html", "layouts/baseof.html")
b.AssertFileContent("public/amp/index.html", "layouts/baseof.amp.html")
}

View file

@ -301,7 +301,7 @@ func (ti *TemplInfo) String() string {
return ti.PathInfo.String() return ti.PathInfo.String()
} }
func (ti *TemplInfo) findBestMatchBaseof(s *TemplateStore, k1 string, slashCountK1 int, best *bestMatch) { func (ti *TemplInfo) findBestMatchBaseof(s *TemplateStore, d1 TemplateDescriptor, k1 string, slashCountK1 int, best *bestMatch) {
if ti.baseVariants == nil { if ti.baseVariants == nil {
return return
} }
@ -310,11 +310,11 @@ func (ti *TemplInfo) findBestMatchBaseof(s *TemplateStore, k1 string, slashCount
slashCountK2 := strings.Count(k2, "/") slashCountK2 := strings.Count(k2, "/")
distance := slashCountK1 - slashCountK2 distance := slashCountK1 - slashCountK2
for d, vv := range v { for d2, vv := range v {
weight := s.dh.compareDescriptors(CategoryBaseof, ti.D, d) weight := s.dh.compareDescriptors(CategoryBaseof, d1, d2)
weight.distance = distance weight.distance = distance
if best.isBetter(weight, vv.Template) { if best.isBetter(weight, vv.Template) {
best.updateValues(weight, k2, d, vv.Template) best.updateValues(weight, k2, d2, vv.Template)
} }
} }
return false, nil return false, nil
@ -538,7 +538,7 @@ func (s *TemplateStore) LookupPagesLayout(q TemplateQuery) *TemplInfo {
return m return m
} }
best1.reset() best1.reset()
m.findBestMatchBaseof(s, key, slashCountKey, best1) m.findBestMatchBaseof(s, q.Desc, key, slashCountKey, best1)
if best1.w.w1 <= 0 { if best1.w.w1 <= 0 {
return nil return nil
} }