Document taxonomies Page method

This commit is contained in:
Joe Mooring 2024-04-16 08:30:18 -07:00 committed by GitHub
parent 05494b7d2c
commit 3aa75ee740
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 30 deletions

View File

@ -0,0 +1,26 @@
---
title: Page
description: Returns the taxonomy page or nil if the taxonomy has no terms.
categories: []
keywords: []
action:
related: []
returnType: page.Page
signatures: [TAXONOMY.Page]
---
{{< new-in 0.125.0 >}}
This `TAXONOMY` method returns nil if the taxonomy has no terms, so you must code defensively:
```go-html-template
{{ with .Site.Taxonomies.tags.Page }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
```
This is rendered to:
```html
<a href="/tags/">Tags</a>
```

View File

@ -36,24 +36,8 @@ See [Template Lookup](/templates/lookup-order/).
### Taxonomy methods ### Taxonomy methods
A Taxonomy is a `map[string]WeightedPages`. {{< list-pages-in-section path=/methods/taxonomy/ >}}
.Get TERM
: Returns the WeightedPages for a given term. For example: ;
`site.Taxonomies.tags.Get "tag-a"`.
.Count TERM
: The number of pieces of content assigned to the given term. For example: \
`site.Taxonomies.tags.Count "tag-a"`.
.Alphabetical
: Returns an OrderedTaxonomy (slice) ordered by term.
.ByCount
: Returns an OrderedTaxonomy (slice) ordered by number of entries.
.Reverse
: Returns an OrderedTaxonomy (slice) in reverse order. Must be used with an OrderedTaxonomy.
### OrderedTaxonomy ### OrderedTaxonomy
@ -273,14 +257,20 @@ The following example displays all terms in a site's tags taxonomy:
This example will list all taxonomies and their terms, as well as all the content assigned to each of the terms. This example will list all taxonomies and their terms, as well as all the content assigned to each of the terms.
{{< code file=layouts/partials/all-taxonomies.html >}} {{< code file=layouts/partials/all-taxonomies.html >}}
<ul> {{ with .Site.Taxonomies }}
{{ range $taxonomy, $terms := site.Taxonomies }} {{ $numberOfTerms := 0 }}
<li> {{ range $taxonomy, $terms := . }}
{{ with site.GetPage $taxonomy }} {{ $numberOfTerms = len . | add $numberOfTerms }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }} {{ end }}
{{ if gt $numberOfTerms 0 }}
<ul> <ul>
{{ range $term, $weightedPages := $terms }} {{ range $taxonomy, $terms := . }}
{{ with $terms }}
<li>
<a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
<ul>
{{ range $term, $weightedPages := . }}
<li> <li>
<a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a> <a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
<ul> <ul>
@ -293,7 +283,10 @@ This example will list all taxonomies and their terms, as well as all the conten
</ul> </ul>
</li> </li>
{{ end }} {{ end }}
</ul> {{ end }}
</ul>
{{ end }}
{{ end }}
{{< /code >}} {{< /code >}}
## `.Site.GetPage` for taxonomies ## `.Site.GetPage` for taxonomies