Skip to content

Commit 51dd462

Browse files
committed
layout: Respect Type and Layout for list template selection
This commit also has some other nice side-effects: * The layout logic is unified for all page types, which should make it less surprising * Page.Render now supports all types * The legacy "indexes" type is removed from the template lookup order. This is an undocumented type from early Hugo days. This means that having a template in, say, `/layouts/indexes/list.html` will no longer work. * The theme override logic is improved. As an example, an `index.html` in theme will now wn over a `_default/list.html` in the project, which most will expect. Fixes #3005 Fixes #3245
1 parent b6ea6d0 commit 51dd462

File tree

6 files changed

+216
-221
lines changed

6 files changed

+216
-221
lines changed

‎hugolib/page_output.go‎

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,19 @@ func (p *PageOutput) layouts(layouts ...string) ([]string, error) {
100100
return []string{p.selfLayout}, nil
101101
}
102102

103-
layoutOverride := ""
103+
layoutDescriptor := p.layoutDescriptor
104+
104105
if len(layouts) > 0 {
105-
layoutOverride = layouts[0]
106+
layoutDescriptor.Layout = layouts[0]
107+
layoutDescriptor.LayoutOverride = true
106108
}
107109

108110
return p.s.layoutHandler.For(
109-
p.layoutDescriptor,
110-
layoutOverride,
111+
layoutDescriptor,
111112
p.outputFormat)
112113
}
113114

114115
func (p *PageOutput) Render(layout ...string) template.HTML {
115-
if !p.checkRender() {
116-
return ""
117-
}
118-
119116
l, err := p.layouts(layout...)
120117
if err != nil {
121118
helpers.DistinctErrorLog.Printf("in .Render: Failed to resolve layout %q for page %q", layout, p.pathOrTitle())
@@ -145,10 +142,6 @@ func (p *PageOutput) Render(layout ...string) template.HTML {
145142
}
146143

147144
func (p *Page) Render(layout ...string) template.HTML {
148-
if !p.checkRender() {
149-
return ""
150-
}
151-
152145
p.pageOutputInit.Do(func() {
153146
if p.mainPageOutput != nil {
154147
return
@@ -170,16 +163,6 @@ func (p *Page) Render(layout ...string) template.HTML {
170163
return p.mainPageOutput.Render(layout...)
171164
}
172165

173-
// We may fix this in the future, but the layout handling in Render isn't built
174-
// for list pages.
175-
func (p *Page) checkRender() bool {
176-
if p.Kind != KindPage {
177-
helpers.DistinctWarnLog.Printf(".Render only available for regular pages, not for of kind %q. You probably meant .Site.RegularPages and not.Site.Pages.", p.Kind)
178-
return false
179-
}
180-
return true
181-
}
182-
183166
// OutputFormats holds a list of the relevant output formats for a given resource.
184167
type OutputFormats []*OutputFormat
185168

‎hugolib/site.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ func (s *Site) kindFromSections(sections []string) string {
15411541
}
15421542

15431543
func (s *Site) layouts(p *PageOutput) ([]string, error) {
1544-
return s.layoutHandler.For(p.layoutDescriptor, "", p.outputFormat)
1544+
return s.layoutHandler.For(p.layoutDescriptor, p.outputFormat)
15451545
}
15461546

15471547
func (s *Site) preparePages() error {

‎hugolib/site_render.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ func (s *Site) renderRSS(p *PageOutput) error {
242242

243243
layouts, err := s.layoutHandler.For(
244244
p.layoutDescriptor,
245-
"",
246245
p.outputFormat)
247246
if err != nil {
248247
return err

‎output/docshelper.go‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,26 @@ func createLayoutExamples() interface{} {
3737
)
3838

3939
for _, example := range []struct {
40-
name string
41-
d LayoutDescriptor
42-
hasTheme bool
43-
layoutOverride string
44-
f Format
40+
name string
41+
d LayoutDescriptor
42+
hasTheme bool
43+
f Format
4544
}{
46-
{`AMP home, with theme "demoTheme".`, LayoutDescriptor{Kind: "home"}, true, "", AMPFormat},
47-
{`AMP home, French language".`, LayoutDescriptor{Kind: "home", Lang: "fr"}, false, "", AMPFormat},
48-
{"RSS home, no theme.", LayoutDescriptor{Kind: "home"}, false, "", RSSFormat},
49-
{"JSON home, no theme.", LayoutDescriptor{Kind: "home"}, false, "", JSONFormat},
50-
{fmt.Sprintf(`CSV regular, "layout: %s" in front matter.`, demoLayout), LayoutDescriptor{Kind: "page", Layout: demoLayout}, false, "", CSVFormat},
51-
{fmt.Sprintf(`JSON regular, "type: %s" in front matter.`, demoType), LayoutDescriptor{Kind: "page", Type: demoType}, false, "", JSONFormat},
52-
{"HTML regular.", LayoutDescriptor{Kind: "page"}, false, "", HTMLFormat},
53-
{"AMP regular.", LayoutDescriptor{Kind: "page"}, false, "", AMPFormat},
54-
{"Calendar blog section.", LayoutDescriptor{Kind: "section", Section: "blog"}, false, "", CalendarFormat},
55-
{"Calendar taxonomy list.", LayoutDescriptor{Kind: "taxonomy", Section: "tag"}, false, "", CalendarFormat},
56-
{"Calendar taxonomy term.", LayoutDescriptor{Kind: "taxonomyTerm", Section: "tag"}, false, "", CalendarFormat},
45+
{`AMP home, with theme "demoTheme".`, LayoutDescriptor{Kind: "home"}, true, AMPFormat},
46+
{`AMP home, French language".`, LayoutDescriptor{Kind: "home", Lang: "fr"}, false, AMPFormat},
47+
{"RSS home, no theme.", LayoutDescriptor{Kind: "home"}, false, RSSFormat},
48+
{"JSON home, no theme.", LayoutDescriptor{Kind: "home"}, false, JSONFormat},
49+
{fmt.Sprintf(`CSV regular, "layout: %s" in front matter.`, demoLayout), LayoutDescriptor{Kind: "page", Layout: demoLayout}, false, CSVFormat},
50+
{fmt.Sprintf(`JSON regular, "type: %s" in front matter.`, demoType), LayoutDescriptor{Kind: "page", Type: demoType}, false, JSONFormat},
51+
{"HTML regular.", LayoutDescriptor{Kind: "page"}, false, HTMLFormat},
52+
{"AMP regular.", LayoutDescriptor{Kind: "page"}, false, AMPFormat},
53+
{"Calendar blog section.", LayoutDescriptor{Kind: "section", Section: "blog"}, false, CalendarFormat},
54+
{"Calendar taxonomy list.", LayoutDescriptor{Kind: "taxonomy", Section: "tag"}, false, CalendarFormat},
55+
{"Calendar taxonomy term.", LayoutDescriptor{Kind: "taxonomyTerm", Section: "tag"}, false, CalendarFormat},
5756
} {
5857

5958
l := NewLayoutHandler(example.hasTheme)
60-
layouts, _ := l.For(example.d, example.layoutOverride, example.f)
59+
layouts, _ := l.For(example.d, example.f)
6160

6261
basicExamples = append(basicExamples, Example{
6362
Example: example.name,

0 commit comments

Comments
 (0)