Taxomony Templates

[TOC]

Übersetzt von gohugo.io

Die Taxonomie-Vorlagen umfassen

  • Taxonomie-Listenseiten,
  • Seiten mit Taxonomiebegriffen und
  • die Verwendung von Taxonomien in den Einzelseitenvorlagen.

Hugo unterstützt benutzerdefinierte Gruppierungen von Inhalten namens taxonomies. Taxonomien sind Klassifizierungen, die logische Zusammenhänge zwischen Inhalten aufzeigen. Siehe Taxonomien unter Content Management, um zu erfahren, wie Hugo diese leistungsstarke Funktion nutzt.

Hugo bietet mehrere Möglichkeiten, Taxonomien in Ihren Projektvorlagen zu verwenden:

Taxonomy List Templates

Seitenvorlagen für Taxonomielisten sind Listen und verfügen daher über alle Variablen und Methoden, die für Listenseiten (list pages) verfügbar sind.

Taxonomy List Template Lookup Order

Siehe Template Lookup.

Taxonomy Terms Template

Taxonomy Terms Templates Lookup Order

Sieh Template Lookup.

Taxonomy Methods

A Taxonomy is a map[string]WeightedPages.

  • .Get(term)

    Returns the WeightedPages for a term.

  • .Count(term)

    The number of pieces of content assigned to this term.

  • .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

Da Maps ungeordnet sind, ist eine OrderedTaxonomy eine spezielle Struktur, die eine definierte Reihenfolge hat.

[]struct {
    Name          string
    WeightedPages WeightedPages
}

Each element of the slice has:

  • .Term

    The Term used.

  • .WeightedPages

    A slice of Weighted Pages.

  • .Count

    The number of pieces of content assigned to this term.

  • .Pages

    All Pages assigned to this term. All list methods are available to this.

WeightedPages

WeightedPages is simply a slice of WeightedPage.

type WeightedPages []WeightedPage
  • .Count(term)

    The number of pieces of content assigned to this term.

  • .Pages

    Returns a slice of pages, which then can be ordered using any of the list methods.

Displaying custom metadata in Taxonomy Terms Templates

Wenn benutzerdefinierte Metadaten für jeden Taxonomiebegriff angezeigt werden sollen, muss eine Seite für diesen Begriff unter /content/<TAXONOMY>/<TERM>/_index.md erstellt werden und die Metadaten in der ersten Zeile hinzufügt werden - wie in der Taxonomiedokumentation erläutert.

Basierend auf dem dort gezeigten Beispiel der Schauspieler-Taxonomie kann innerhalb der Vorlage für Taxonomiebegriffe auf die benutzerdefinierten Felder zugegriffen werden, indem über die Variable .Pages als solche iteriert wird.

<ul>
    {{ range .Pages }}
        <li>
            <a href="{{ .Permalink }}">{{ .Title }}</a>
            {{ .Params.wikipedia }}
        </li>
    {{ end }}
</ul>

Order Taxonomies

Taxonomien können entweder nach alphabetischem Schlüssel oder nach der Anzahl der diesem Schlüssel zugeordneten Inhaltselemente geordnet werden.

Order Alphabetically Example

In Hugo 0.55 and later you can do:

<ul>
    {{ range .Data.Terms.Alphabetical }}
            <li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> {{ .Count }}</li>
    {{ end }}
</ul>

Vorher muss sowas wie hier getan werden:

<ul>
    {{ $type := .Type }}
    {{ range $key, $value := .Data.Terms.Alphabetical }}
        {{ $name := .Name }}
        {{ $count := .Count }}
        {{ with $.Site.GetPage (printf "/%s/%s" $type $name) }}
            <li><a href="{{ .Permalink }}">{{ $name }}</a> {{ $count }}</li>
        {{ end }}
    {{ end }}
</ul>

Order Content within Taxonomies

Hugo verwendet sowohl date als auch weight (Datum und Gewichung), um Inhalte innerhalb von Taxonomien zu ordnen.

Jedem Inhalt in Hugo kann optional ein Datum zugeordnet werden. Sie kann auch für jede Taxonomie, der sie zugeordnet ist, eine Gewichtung erhalten.

Wenn über Inhalte in Taxonomien iteriert wird, ist die Standardsortierung die gleiche wie bei den Seiten Abschnitt und Liste zuerst nach Gewicht und dann nach Datum. Das bedeutet, wenn die Gewichte für zwei Inhalte gleich sind, wird zuerst der aktuellere Inhalt angezeigt. Das Standardgewicht für jeden Inhalt ist 0.

Assign Weight

Der Inhalt kann für jede Taxonomie, der er zugeordnet ist, gewichtet werden.

+++
tags = [ "a", "b", "c" ]
tags_weight = 22
categories = ["d"]
title = "foo"
categories_weight = 44
+++
Front Matter with weighted tags and categories

Die Konvention ist taxonomyname_weight.

Im obigen Beispiel hat dieser Inhalt ein Gewicht von 22, das für die Sortierung bei der Darstellung der Seiten gilt, die den Werten “a”, “b” und “c” der Taxonomie “tag” zugeordnet sind.

Es wurde auch das Gewicht von 44 zugewiesen, wenn es die Kategorie “d” wiedergibt.

Dadurch kann derselbe Inhalt an verschiedenen Stellen in verschiedenen Taxonomien erscheinen.

Derzeit unterstützen Taxonomien nur die Standardreihenfolge der Inhalte, d.h. Gewicht -> Datum.

Es gibt zwei verschiedene Vorlagen, die für die Verwendung von Taxonomien bereitstellt werden müssen.

Beide Vorlagen werden im Abschnitt Vorlagen ausführlich behandelt.

  1. Eine list template ist jede Vorlage, die verwendet wird, um mehrere Inhalte auf einer einzigen HTML-Seite darzustellen. Diese Vorlage wird verwendet, um alle automatisch erstellten Taxonomie-Seiten zu generieren.
  2. Eine taxonomische Begriffsvorlage ist eine Vorlage, die verwendet wird, um die Liste der Begriffe für eine bestimmte Vorlage zu erstellen.

Es gibt vier gängige Möglichkeiten, die Daten in Ihren Taxonomien anzuzeigen, zusätzlich zu den automatischen Taxonomieseiten, die von Hugo mit Hilfe der list templates erstellt wurden:

  1. Für einen bestimmten Inhalt können die angehängten Begriffe (terms) auflistet werden.
  2. Für einen bestimmten Inhalt können andere Inhalte mit dem gleichen Begriff auflistet werden.
  3. Alle Begriffe können für eine Taxonomie auflistet werden.
  4. Alle Taxonomien (mit ihren Begriffen) können auflistet werden.

Display a Single Piece of Content’s Taxonomies

Within your content templates, you may wish to display the taxonomies that piece of content is assigned to.

Because we are leveraging the front matter system to define taxonomies for content, the taxonomies assigned to each content piece are located in the usual place (i.e., .Params.<TAXONOMYPLURAL>).

Example: List Tags in a Single Page Template

{{ $taxo := "tags" }} <!-- Use the plural form here -->
<ul id="{{ $taxo }}">
    {{ range .Param $taxo }}
        {{ $name := . }}
        {{ with $.Site.GetPage (printf "/%s/%s" $taxo ($name | urlize)) }}
            <li><a href="{{ .Permalink }}">{{ $name }}</a></li>
        {{ end }}
    {{ end }}
</ul>

If you want to list taxonomies inline, you will have to take care of optional plural endings in the title (if multiple taxonomies), as well as commas. Let’s say we have a taxonomy “directors” such as directors: [ "Joel Coen", "Ethan Coen" ] in the TOML-format front matter.

To list such taxonomies, use the following:

Example: Comma-delimit Tags in a Single Page Template

{{ $taxo := "directors" }} <!-- Use the plural form here -->
{{ with .Param $taxo }}
    <strong>Director{{ if gt (len .) 1 }}s{{ end }}:</strong>
    {{ range $index, $director := . }}
        {{- if gt $index 0 }}, {{ end -}}
        {{ with $.Site.GetPage (printf "/%s/%s" $taxo $director) -}}
            <a href="{{ .Permalink }}">{{ $director }}</a>
        {{- end -}}
    {{- end -}}
{{ end }}

Alternatively, you may use the delimit template function as a shortcut if the taxonomies should just be listed with a separator. See #2143 on GitHub for discussion.

List Content with the Same Taxonomy Term

If you are using a taxonomy for something like a series of posts, you can list individual pages associated with the same taxonomy. This is also a quick and dirty method for showing related content:

Example: Showing Content in Same Series

<ul>
    {{ range .Site.Taxonomies.series.golang }}
        <li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
    {{ end }}
</ul>

List All content in a Given taxonomy

This would be very useful in a sidebar as “featured content”. You could even have different sections of “featured content” by assigning different terms to the content.

<section id="menu">
    <ul>
        {{ range $key, $taxonomy := .Site.Taxonomies.featured }}
        <li>{{ $key }}</li>
        <ul>
            {{ range $taxonomy.Pages }}
            <li hugo-nav="{{ .RelPermalink}}"><a href="{{ .Permalink}}">{{ .LinkTitle }}</a></li>
            {{ end }}
        </ul>
        {{ end }}
    </ul>
</section>

Render a Site’s Taxonomies

If you wish to display the list of all keys for your site’s taxonomy, you can retrieve them from the .Site variable available on every page.

This may take the form of a tag cloud, a menu, or simply a list.

The following example displays all terms in a site’s tags taxonomy:

Example: List All Site Tags

In Hugo 0.55 and later you can simply do:

<ul>
    {{ range .Site.Taxonomies.tags }}
            <li><a href="{{ .Page.Permalink }}">{{ .Page.Title }}</a> {{ .Count }}</li>
    {{ end }}
</ul>

Before that you would do something like this:

<ul id="all-tags">
    {{ range $name, $taxonomy := .Site.Taxonomies.tags }}
        {{ with $.Site.GetPage (printf "/tags/%s" $name) }}
            <li><a href="{{ .Permalink }}">{{ $name }}</a></li>
        {{ end }}
    {{ end }}
</ul>

Example: List All Taxonomies, Terms, and Assigned Content

This example will list all taxonomies and their terms, as well as all the content assigned to each of the terms.

layouts/partials/all-taxonomies.html

<section>
    <ul id="all-taxonomies">
        {{ range $taxonomy_term, $taxonomy := .Site.Taxonomies }}
            {{ with $.Site.GetPage (printf "/%s" $taxonomy_term) }}
                <li><a href="{{ .Permalink }}">{{ $taxonomy_term }}</a>
                    <ul>
                        {{ range $key, $value := $taxonomy }}
                            <li>{{ $key }}</li>
                            <ul>
                                {{ range $value.Pages }}
                                    <li hugo-nav="{{ .RelPermalink}}">
                                        <a href="{{ .Permalink}}">{{ .LinkTitle }}</a>
                                    </li>
                                {{ end }}
                            </ul>
                        {{ end }}
                    </ul>
                </li>
            {{ end }}
        {{ end }}
    </ul>
</section>

.Site.GetPage for Taxonomies

Because taxonomies are lists, the .GetPage function can be used to get all the pages associated with a particular taxonomy term using a terse syntax. The following ranges over the full list of tags on your site and links to each of the individual taxonomy pages for each term without having to use the more fragile URL construction of the “List All Site Tags” example above:

links-to-all-tags.html

{{ $taxo := "tags" }}
<ul class="{{ $taxo }}">
    {{ with ($.Site.GetPage (printf "/%s" $taxo)) }}
        {{ range .Pages }}
            <li><a href="{{ .Permalink }}">{{ .Title}}</a></li>
        {{ end }}
    {{ end }}
</ul>

See Also