mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-18 21:11:19 +02:00
tpl/collections: Unwrap any interface value in sort and where
Hugo `0.55.0` introduced some new interface types for `Page` etc. This worked great in general, but there were cases where this would fail in `where` and `sort`. One such example would be sorting by `MenuItem.Page.Date` where `Page` on `MenuItem` was a small subset of the bigger `page.Page` interface. This commit fixes that by unwrapping such interface values. Fixes #5989
This commit is contained in:
@@ -222,3 +222,54 @@ menu: "main"
|
||||
b.AssertFileContent("public/index.html", "AMP and HTML|/blog/html-amp/|AMP only|/amp/blog/amp/|HTML only|/blog/html/|Home Sweet Home|/|")
|
||||
b.AssertFileContent("public/amp/index.html", "AMP and HTML|/amp/blog/html-amp/|AMP only|/amp/blog/amp/|HTML only|/blog/html/|Home Sweet Home|/amp/|")
|
||||
}
|
||||
|
||||
// https://github.com/gohugoio/hugo/issues/5989
|
||||
func TestMenuPageSortByDate(t *testing.T) {
|
||||
|
||||
b := newTestSitesBuilder(t).WithSimpleConfigFile()
|
||||
|
||||
b.WithContent("blog/a.md", `
|
||||
---
|
||||
Title: A
|
||||
date: 2019-01-01
|
||||
menu:
|
||||
main:
|
||||
identifier: "a"
|
||||
weight: 1
|
||||
---
|
||||
|
||||
`)
|
||||
|
||||
b.WithContent("blog/b.md", `
|
||||
---
|
||||
Title: B
|
||||
date: 2018-01-02
|
||||
menu:
|
||||
main:
|
||||
parent: "a"
|
||||
weight: 100
|
||||
---
|
||||
|
||||
`)
|
||||
|
||||
b.WithContent("blog/c.md", `
|
||||
---
|
||||
Title: C
|
||||
date: 2019-01-03
|
||||
menu:
|
||||
main:
|
||||
parent: "a"
|
||||
weight: 10
|
||||
---
|
||||
|
||||
`)
|
||||
|
||||
b.WithTemplatesAdded("index.html", `{{ range .Site.Menus.main }}{{ .Title }}|Children:
|
||||
{{- $children := sort .Children ".Page.Date" "desc" }}{{ range $children }}{{ .Title }}|{{ end }}{{ end }}
|
||||
|
||||
`)
|
||||
|
||||
b.Build(BuildCfg{})
|
||||
|
||||
b.AssertFileContent("public/index.html", "A|Children:C|B|")
|
||||
}
|
||||
|
Reference in New Issue
Block a user