Skip to content

Commit 9221cbc

Browse files
committed
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
1 parent e3e3f9a commit 9221cbc

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

Diff for: tpl/templates/templates_integration_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,31 @@ p3.current.Ancestors.Reverse: {{ with templates.Current }}{{ range .Ancestors.Re
166166
"p2.current.Ancestors: _partials/p1.html|all.html",
167167
)
168168
}
169+
170+
func TestBaseOfIssue13583(t *testing.T) {
171+
t.Parallel()
172+
173+
files := `
174+
-- hugo.toml --
175+
-- content/_index.md --
176+
---
177+
title: "Home"
178+
outputs: ["html", "amp"]
179+
---
180+
title: "Home"
181+
-- layouts/baseof.html --
182+
layouts/baseof.html
183+
{{ block "main" . }}{{ end }}
184+
-- layouts/baseof.amp.html --
185+
layouts/baseof.amp.html
186+
{{ block "main" . }}{{ end }}
187+
-- layouts/home.html --
188+
{{ define "main" }}
189+
Home.
190+
{{ end }}
191+
192+
`
193+
b := hugolib.Test(t, files)
194+
b.AssertFileContent("public/index.html", "layouts/baseof.html")
195+
b.AssertFileContent("public/amp/index.html", "layouts/baseof.amp.html")
196+
}

Diff for: tpl/tplimpl/templatestore.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (ti *TemplInfo) String() string {
301301
return ti.PathInfo.String()
302302
}
303303

304-
func (ti *TemplInfo) findBestMatchBaseof(s *TemplateStore, k1 string, slashCountK1 int, best *bestMatch) {
304+
func (ti *TemplInfo) findBestMatchBaseof(s *TemplateStore, d1 TemplateDescriptor, k1 string, slashCountK1 int, best *bestMatch) {
305305
if ti.baseVariants == nil {
306306
return
307307
}
@@ -310,11 +310,11 @@ func (ti *TemplInfo) findBestMatchBaseof(s *TemplateStore, k1 string, slashCount
310310
slashCountK2 := strings.Count(k2, "/")
311311
distance := slashCountK1 - slashCountK2
312312

313-
for d, vv := range v {
314-
weight := s.dh.compareDescriptors(CategoryBaseof, ti.D, d)
313+
for d2, vv := range v {
314+
weight := s.dh.compareDescriptors(CategoryBaseof, d1, d2)
315315
weight.distance = distance
316316
if best.isBetter(weight, vv.Template) {
317-
best.updateValues(weight, k2, d, vv.Template)
317+
best.updateValues(weight, k2, d2, vv.Template)
318318
}
319319
}
320320
return false, nil
@@ -538,7 +538,7 @@ func (s *TemplateStore) LookupPagesLayout(q TemplateQuery) *TemplInfo {
538538
return m
539539
}
540540
best1.reset()
541-
m.findBestMatchBaseof(s, key, slashCountKey, best1)
541+
m.findBestMatchBaseof(s, q.Desc, key, slashCountKey, best1)
542542
if best1.w.w1 <= 0 {
543543
return nil
544544
}

0 commit comments

Comments
 (0)