]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl: Fix some baseof lookup issues
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 11 Apr 2025 08:16:28 +0000 (10:16 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 11 Apr 2025 16:23:51 +0000 (18:23 +0200)
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
tpl/tplimpl/templatestore.go

index a0dcf03488ab900a98dbe08134ab493a57c5443a..7527095931d4bf317acbd216197047261981aade 100644 (file)
@@ -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")
+}
index eee962053955897af0629349f719cc4fa9fd06df..3c8c4abf23577e0ec2b6fcd3147c933a258b7dee 100644 (file)
@@ -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
        }