ref: fa520a2d983b982394ad10088393fb303e48980a
parent: 82029c1ec975bc2173bd5a454aee6c800924035d
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Wed Feb 19 04:16:27 EST 2020
Add Page.GetTerms Fixes #6905
--- a/docs/content/en/templates/taxonomy-templates.md
+++ b/docs/content/en/templates/taxonomy-templates.md
@@ -216,6 +216,18 @@
### Example: List Tags in a Single Page Template
+{{< new-in "0.65.0" >}}+
+```go-html-template
+<ul>
+ {{ range (.GetTerms "tags") }}+ <li><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></li>+ {{ end }}+</ul>
+```
+
+Before Hugo 0.65.0 you needed to do something like this:
+
```go-html-template
{{ $taxo := "tags" }} <!-- Use the plural form here --> <ul id="{{ $taxo }}">--- a/hugolib/content_map_page.go
+++ b/hugolib/content_map_page.go
@@ -599,7 +599,7 @@
if s == "/" {// To avoid getting an empty key.
- s = "home"
+ s = page.KindHome
}
key := cleanTreeKey(path.Join(viewName.plural, termKey, s))
m.taxonomyEntries.Insert(key, bv)
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -131,6 +131,34 @@
return p.gitInfo
}
+// GetTerms gets the terms defined on this page in the given taxonomy.
+func (p *pageState) GetTerms(taxonomy string) page.Pages {+ taxonomy = strings.ToLower(taxonomy)
+ m := p.s.pageMap
+ prefix := cleanTreeKey(taxonomy)
+
+ var self string
+ if p.IsHome() {+ // TODO(bep) make this less magical, see taxonomyEntries.Insert.
+ self = "/" + page.KindHome
+ } else {+ self = p.treeRef.key
+ }
+
+ var pas page.Pages
+
+ m.taxonomies.WalkPrefixListable(prefix, func(s string, n *contentNode) bool {+ if _, found := m.taxonomyEntries.Get(s + self); found {+ pas = append(pas, n.p)
+ }
+ return false
+ })
+
+ page.SortByDefault(pas)
+
+ return pas
+}
+
func (p *pageState) MarshalJSON() ([]byte, error) {return page.MarshalPageToJSON(p)
}
--- a/hugolib/site_benchmark_new_test.go
+++ b/hugolib/site_benchmark_new_test.go
@@ -378,14 +378,10 @@
{"List terms", func(b testing.TB) *sitesBuilder {pageTemplateTemplate := `
-{{ $taxo := "categories" }}<ul>
- {{ range .Param $taxo }}- {{ $name := . }}- {{ with $.Site.GetPage (printf "/%s/%s" $taxo ($name | urlize)) }}- <li><a href="{{ .Permalink }}">{{ $name }}</a></li>- {{ end }}- {{ end }}+ {{ range (.GetTerms "categories") }}+ <li><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></li>+ {{ end }}</ul>
`
--- a/hugolib/taxonomy_test.go
+++ b/hugolib/taxonomy_test.go
@@ -542,11 +542,22 @@
t.Parallel()
b := newTestSitesBuilder(t)
- b.WithContent("p1.md", `---+ b.WithContent(
+ "_index.md", `---
+title: "Home Sweet Home"
+categories: [ "dogs", "gorillas"]
+---
+`,
+ "section/_index.md", `---
+title: "Section"
+categories: [ "cats", "dogs", "birds"]
+---
+`,
+ "section/p1.md", `---
title: "Page1"
categories: ["funny", "cats"]
---
-`, "p2.md", `---
+`, "section/p2.md", `---
title: "Page2"
categories: ["funny"]
---
@@ -553,14 +564,19 @@
`)
b.WithTemplatesAdded("index.html", `+{{ $home := site.Home }}+{{ $section := site.GetPage "section" }} {{ $categories := site.GetPage "categories" }} {{ $funny := site.GetPage "categories/funny" }} {{ $cats := site.GetPage "categories/cats" }}+{{ $p1 := site.GetPage "section/p1" }} Categories Pages: {{ range $categories.Pages}}{{.RelPermalink }}|{{ end }}:END Funny Pages: {{ range $funny.Pages}}{{.RelPermalink }}|{{ end }}:END Cats Pages: {{ range $cats.Pages}}{{.RelPermalink }}|{{ end }}:END-
+P1 Terms: {{ range $p1.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END+Section Terms: {{ range $section.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END+Home Terms: {{ range $home.GetTerms "categories" }}{{.RelPermalink }}|{{ end }}:END`)
b.Build(BuildCfg{})@@ -575,12 +591,15 @@
b.Assert(funny.Parent(), qt.Equals, cat)
b.AssertFileContent("public/index.html", `-Categories Pages: /categories/cats/|/categories/funny/|:END
-Funny Pages: /p1/|/p2/|:END
-Cats Pages: /p1/|:END
+ Categories Pages: /categories/birds/|/categories/cats/|/categories/dogs/|/categories/funny/|/categories/gorillas/|:END
+ Funny Pages: /section/p1/|/section/p2/|:END
+ Cats Pages: /section/p1/|/section/|:END
+ P1 Terms: /categories/cats/|/categories/funny/|:END
+ Section Terms: /categories/birds/|/categories/cats/|/categories/dogs/|:END
+ Home Terms: /categories/dogs/|/categories/gorillas/|:END
`)
- b.AssertFileContent("public/categories/funny/index.xml", `<link>http://example.com/p1/</link>`)+ b.AssertFileContent("public/categories/funny/index.xml", `<link>http://example.com/section/p1/</link>`) b.AssertFileContent("public/categories/index.xml", `<link>http://example.com/categories/funny/</link>`)}
--- a/resources/page/page.go
+++ b/resources/page/page.go
@@ -252,6 +252,10 @@
maps.Scratcher
RelatedKeywordsProvider
+ // GetTerms gets the terms of a given taxonomy,
+ // e.g. GetTerms("categories")+ GetTerms(taxonomy string) Pages
+
DeprecatedWarningPageMethods
}
--- a/resources/page/page_nop.go
+++ b/resources/page/page_nop.go
@@ -174,6 +174,10 @@
return nil
}
+func (p *nopPage) GetTerms(taxonomy string) Pages {+ return nil
+}
+
func (p *nopPage) GitInfo() *gitmap.GitInfo {return nil
}
--- a/resources/page/testhelpers_test.go
+++ b/resources/page/testhelpers_test.go
@@ -222,6 +222,10 @@
panic("not implemented")}
+func (p *testPage) GetTerms(taxonomy string) Pages {+ panic("not implemented")+}
+
func (p *testPage) GetRelatedDocsHandler() *RelatedDocsHandler {return relatedDocsHandler
}
--
⑨