From: Bjørn Erik Pedersen Date: Fri, 11 Apr 2025 08:16:28 +0000 (+0200) Subject: tpl: Fix some baseof lookup issues X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=9221cbca496752fb1d06d664871e3d4532f473f5;p=brevno-suite%2Fhugo 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 --- diff --git a/tpl/templates/templates_integration_test.go b/tpl/templates/templates_integration_test.go index a0dcf0348..752709593 100644 --- a/tpl/templates/templates_integration_test.go +++ b/tpl/templates/templates_integration_test.go @@ -166,3 +166,31 @@ p3.current.Ancestors.Reverse: {{ with templates.Current }}{{ range .Ancestors.Re "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") +} diff --git a/tpl/tplimpl/templatestore.go b/tpl/tplimpl/templatestore.go index eee962053..3c8c4abf2 100644 --- a/tpl/tplimpl/templatestore.go +++ b/tpl/tplimpl/templatestore.go @@ -301,7 +301,7 @@ func (ti *TemplInfo) String() 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 { return } @@ -310,11 +310,11 @@ func (ti *TemplInfo) findBestMatchBaseof(s *TemplateStore, k1 string, slashCount slashCountK2 := strings.Count(k2, "/") distance := slashCountK1 - slashCountK2 - for d, vv := range v { - weight := s.dh.compareDescriptors(CategoryBaseof, ti.D, d) + for d2, vv := range v { + weight := s.dh.compareDescriptors(CategoryBaseof, d1, d2) weight.distance = distance if best.isBetter(weight, vv.Template) { - best.updateValues(weight, k2, d, vv.Template) + best.updateValues(weight, k2, d2, vv.Template) } } return false, nil @@ -538,7 +538,7 @@ func (s *TemplateStore) LookupPagesLayout(q TemplateQuery) *TemplInfo { return m } best1.reset() - m.findBestMatchBaseof(s, key, slashCountKey, best1) + m.findBestMatchBaseof(s, q.Desc, key, slashCountKey, best1) if best1.w.w1 <= 0 { return nil }