Skip to content

Commit

Permalink
Add Page.GetTerms
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 19, 2020
1 parent 82029c1 commit 9e7e48d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 15 deletions.
12 changes: 12 additions & 0 deletions docs/content/en/templates/taxonomy-templates.md
Expand Up @@ -216,6 +216,18 @@ Because we are leveraging the front matter system to define taxonomies for conte

### 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 }}">
Expand Down
2 changes: 1 addition & 1 deletion hugolib/content_map_page.go
Expand Up @@ -599,7 +599,7 @@ func (m *pageMap) attachPageToViews(s string, b *contentNode) {

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)
Expand Down
28 changes: 28 additions & 0 deletions hugolib/page.go
Expand Up @@ -131,6 +131,34 @@ func (p *pageState) GitInfo() *gitmap.GitInfo {
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)
}
Expand Down
10 changes: 3 additions & 7 deletions hugolib/site_benchmark_new_test.go
Expand Up @@ -378,14 +378,10 @@ contentDir="content/sv"
{"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>
`

Expand Down
33 changes: 26 additions & 7 deletions hugolib/taxonomy_test.go
Expand Up @@ -542,25 +542,41 @@ func TestTaxonomiesPageCollections(t *testing.T) {
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"]
---
`)

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{})
Expand All @@ -575,12 +591,15 @@ Cats Pages: {{ range $cats.Pages}}{{.RelPermalink }}|{{ end }}:END
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>`)

}
4 changes: 4 additions & 0 deletions resources/page/page.go
Expand Up @@ -252,6 +252,10 @@ type PageWithoutContent interface {
maps.Scratcher
RelatedKeywordsProvider

// GetTerms gets the terms of a given taxonomy,
// e.g. GetTerms("categories")
GetTerms(taxonomy string) Pages

DeprecatedWarningPageMethods
}

Expand Down
4 changes: 4 additions & 0 deletions resources/page/page_nop.go
Expand Up @@ -174,6 +174,10 @@ func (p *nopPage) GetParam(key string) interface{} {
return nil
}

func (p *nopPage) GetTerms(taxonomy string) Pages {
return nil
}

func (p *nopPage) GitInfo() *gitmap.GitInfo {
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions resources/page/testhelpers_test.go
Expand Up @@ -222,6 +222,10 @@ func (p *testPage) GetParam(key string) interface{} {
panic("not implemented")
}

func (p *testPage) GetTerms(taxonomy string) Pages {
panic("not implemented")
}

func (p *testPage) GetRelatedDocsHandler() *RelatedDocsHandler {
return relatedDocsHandler
}
Expand Down

0 comments on commit 9e7e48d

Please sign in to comment.