From 9221cbca496752fb1d06d664871e3d4532f473f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 11 Apr 2025 10:16:28 +0200 Subject: [PATCH] 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 --- tpl/templates/templates_integration_test.go | 28 +++++++++++++++++++++ tpl/tplimpl/templatestore.go | 10 ++++---- 2 files changed, 33 insertions(+), 5 deletions(-) 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 }