ref: 8203fc59234610d53dacafc23502edf4ec8c329b
parent: 7acec3c63991f5f3f5907e6e3b3f531d77fa97c8
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Thu Mar 2 05:04:20 EST 2017
hugolib: Add /layouts/SECTION/list.html to template lookup Fixes #3116
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -642,7 +642,7 @@
return p.s.appendThemeTemplates([]string{"index.html", "_default/list.html"})case KindSection:
section := p.sections[0]
- return p.s.appendThemeTemplates([]string{"section/" + section + ".html", "_default/section.html", "_default/list.html", "indexes/" + section + ".html", "_default/indexes.html"})+ return p.s.appendThemeTemplates([]string{"section/" + section + ".html", section + "/list.html", "_default/section.html", "_default/list.html", "indexes/" + section + ".html", "_default/indexes.html"})case KindTaxonomy:
singular := p.s.taxonomiesPluralSingular[p.sections[0]]
return p.s.appendThemeTemplates([]string{"taxonomy/" + singular + ".html", "indexes/" + singular + ".html", "_default/taxonomy.html", "_default/list.html"})--- a/hugolib/template_test.go
+++ b/hugolib/template_test.go
@@ -14,6 +14,7 @@
package hugolib
import (
+ "fmt"
"path/filepath"
"testing"
@@ -23,7 +24,7 @@
"github.com/spf13/viper"
)
-func TestBaseGoTemplate(t *testing.T) {+func TestTemplateLookupOrder(t *testing.T) {t.Parallel()
var (
fs *hugofs.Fs
@@ -31,12 +32,12 @@
th testHelper
)
- // Variants:
+ // Variants base templates:
// 1. <current-path>/<template-name>-baseof.<suffix>, e.g. list-baseof.<suffix>.
// 2. <current-path>/baseof.<suffix>
// 3. _default/<template-name>-baseof.<suffix>, e.g. list-baseof.<suffix>.
// 4. _default/baseof.<suffix>
- for _, this := range []struct {+ for i, this := range []struct {setup func(t *testing.T)
assert func(t *testing.T)
}{@@ -43,12 +44,12 @@
{// Variant 1
func(t *testing.T) {- writeSource(t, fs, filepath.Join("layouts", "section", "sect-baseof.html"), `Base: {{block "main" .}}block{{end}}`)- writeSource(t, fs, filepath.Join("layouts", "section", "sect.html"), `{{define "main"}}sect{{ end }}`)+ writeSource(t, fs, filepath.Join("layouts", "section", "sect1-baseof.html"), `Base: {{block "main" .}}block{{end}}`)+ writeSource(t, fs, filepath.Join("layouts", "section", "sect1.html"), `{{define "main"}}sect{{ end }}`)},
func(t *testing.T) {- th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base: sect")+ th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: sect")},
},
{@@ -70,7 +71,7 @@
},
func(t *testing.T) {- th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base: list")+ th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: list")},
},
{@@ -81,7 +82,7 @@
},
func(t *testing.T) {- th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base: list")+ th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: list")},
},
{@@ -88,13 +89,13 @@
// Variant 1, theme, use project's base
func(t *testing.T) { cfg.Set("theme", "mytheme")- writeSource(t, fs, filepath.Join("layouts", "section", "sect-baseof.html"), `Base: {{block "main" .}}block{{end}}`)+ writeSource(t, fs, filepath.Join("layouts", "section", "sect1-baseof.html"), `Base: {{block "main" .}}block{{end}}`) writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "section", "sect-baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)- writeSource(t, fs, filepath.Join("layouts", "section", "sect.html"), `{{define "main"}}sect{{ end }}`)+ writeSource(t, fs, filepath.Join("layouts", "section", "sect1.html"), `{{define "main"}}sect{{ end }}`)},
func(t *testing.T) {- th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base: sect")+ th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: sect")},
},
{@@ -101,12 +102,12 @@
// Variant 1, theme, use theme's base
func(t *testing.T) { cfg.Set("theme", "mytheme")- writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "section", "sect-baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)- writeSource(t, fs, filepath.Join("layouts", "section", "sect.html"), `{{define "main"}}sect{{ end }}`)+ writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "section", "sect1-baseof.html"), `Base Theme: {{block "main" .}}block{{end}}`)+ writeSource(t, fs, filepath.Join("layouts", "section", "sect1.html"), `{{define "main"}}sect{{ end }}`)},
func(t *testing.T) {- th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base Theme: sect")+ th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base Theme: sect")},
},
{@@ -119,7 +120,7 @@
},
func(t *testing.T) {- th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base: list")+ th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base: list")},
},
{@@ -131,23 +132,48 @@
},
func(t *testing.T) {- th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base Theme: list")+ th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "Base Theme: list")},
},
+ {+ // Test section list and single template selection.
+ // Issue #3116
+ func(t *testing.T) {+ cfg.Set("theme", "mytheme")+
+ // Both single and list template in /SECTION/
+ writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "sect1", "list.html"), `sect list`)+ writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "list.html"), `default list`)+ writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "sect1", "single.html"), `sect single`)+ writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "_default", "single.html"), `default single`)+
+ // sect2 with list template in /section
+ writeSource(t, fs, filepath.Join("themes", "mytheme", "layouts", "section", "sect2.html"), `sect2 list`)+
+ },
+ func(t *testing.T) {+ th.assertFileContent(filepath.Join("public", "sect1", "index.html"), "sect list")+ th.assertFileContent(filepath.Join("public", "sect1", "page1", "index.html"), "sect single")+ th.assertFileContent(filepath.Join("public", "sect2", "index.html"), "sect2 list")+ },
+ },
} {cfg, fs = newTestCfg()
th = testHelper{cfg, fs, t}- writeSource(t, fs, filepath.Join("content", "sect", "page.md"), `---+ for i := 1; i <= 3; i++ {+ writeSource(t, fs, filepath.Join("content", fmt.Sprintf("sect%d", i), fmt.Sprintf("page%d.md", i)), `---title: Template test
---
Some content
`)
+ }
+
this.setup(t)
buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})-
+ t.Log("Template Lookup test", i)this.assert(t)
}
--
⑨