2013-10-31 13:51:13 +00:00
|
|
|
|
---
|
2014-05-29 22:42:05 +00:00
|
|
|
|
aliases:
|
|
|
|
|
- /indexes/displaying/
|
2016-06-30 04:52:29 +00:00
|
|
|
|
lastmod: 2016-06-29
|
2014-05-29 22:42:05 +00:00
|
|
|
|
date: 2013-07-01
|
|
|
|
|
linktitle: Displaying
|
2014-04-23 07:00:11 +00:00
|
|
|
|
menu:
|
|
|
|
|
main:
|
2014-05-29 22:42:05 +00:00
|
|
|
|
parent: taxonomy
|
|
|
|
|
next: /taxonomies/templates
|
|
|
|
|
prev: /taxonomies/usage
|
|
|
|
|
title: Displaying Taxonomies
|
|
|
|
|
weight: 20
|
2015-05-22 18:46:09 +00:00
|
|
|
|
toc: true
|
2013-10-31 13:51:13 +00:00
|
|
|
|
---
|
2014-01-11 02:19:19 +00:00
|
|
|
|
|
2014-05-27 22:32:57 +00:00
|
|
|
|
There are four common ways you can display the data in your
|
|
|
|
|
taxonomies in addition to the automatic taxonomy pages created by hugo
|
2015-01-28 02:17:09 +00:00
|
|
|
|
using the [list templates](/templates/list/):
|
2014-05-27 22:32:57 +00:00
|
|
|
|
|
2014-10-07 23:52:58 +00:00
|
|
|
|
1. For a given piece of content, you can list the terms attached
|
|
|
|
|
2. For a given piece of content, you can list other content with the same
|
2014-05-27 22:32:57 +00:00
|
|
|
|
term
|
|
|
|
|
3. You can list all terms for a taxonomy
|
|
|
|
|
4. You can list all taxonomies (with their terms)
|
|
|
|
|
|
|
|
|
|
## 1. Displaying taxonomy terms assigned to this content
|
2013-10-31 13:51:13 +00:00
|
|
|
|
|
2014-10-07 23:52:58 +00:00
|
|
|
|
Within your content templates, you may wish to display
|
2014-05-27 22:32:57 +00:00
|
|
|
|
the taxonomies that that piece of content is assigned to.
|
2013-10-31 13:51:13 +00:00
|
|
|
|
|
2014-10-07 23:52:58 +00:00
|
|
|
|
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
|
|
|
|
|
(.Params.`plural`).
|
2013-10-31 13:51:13 +00:00
|
|
|
|
|
2014-01-11 02:19:19 +00:00
|
|
|
|
### Example
|
2013-10-31 13:51:13 +00:00
|
|
|
|
|
2014-05-15 13:57:36 +00:00
|
|
|
|
<ul id="tags">
|
|
|
|
|
{{ range .Params.tags }}
|
2016-05-14 04:35:16 +00:00
|
|
|
|
<li><a href="{{ "/tags/" | relLangURL }}{{ . | urlize }}">{{ . }}</a> </li>
|
2014-05-15 13:57:36 +00:00
|
|
|
|
{{ end }}
|
|
|
|
|
</ul>
|
2013-10-31 13:51:13 +00:00
|
|
|
|
|
2016-05-13 07:51:36 +00:00
|
|
|
|
If you want to list taxonomies inline, you will have to take
|
|
|
|
|
care of optional plural ending 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 taxonomy use the following:
|
|
|
|
|
|
|
|
|
|
### Example
|
|
|
|
|
|
|
|
|
|
{{ if .Params.directors }}
|
|
|
|
|
<strong>Director{{ if gt (len .Params.directors) 1 }}s{{ end }}:</strong>
|
2016-08-17 08:30:55 +00:00
|
|
|
|
{{ range $index, $director := .Params.directors }}{{ if gt $index 0 }}, {{ end }}<a href="{{ "/directors/" | relURL }}{{ . | urlize }}">{{ . }}</a>{{ end }}
|
2016-05-13 07:51:36 +00:00
|
|
|
|
{{ end }}
|
|
|
|
|
|
2016-06-30 04:52:29 +00:00
|
|
|
|
Alternatively, you may use the [delimit]({{< relref "templates/functions.md#delimit" >}})
|
|
|
|
|
template function as a shortcut if the taxonomies should just be listed
|
|
|
|
|
with a separator. See {{< gh 2143 >}} on GitHub for discussion.
|
|
|
|
|
|
2014-05-27 22:32:57 +00:00
|
|
|
|
## 2. Listing content with the same taxonomy term
|
|
|
|
|
|
2014-10-07 23:52:58 +00:00
|
|
|
|
First, you may be asking why you would use this. If you are using a
|
2014-05-27 22:32:57 +00:00
|
|
|
|
taxonomy for something like a series of posts, this is exactly how you
|
|
|
|
|
would do it. It’s also an quick and dirty way to show some related
|
|
|
|
|
content.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Example
|
|
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
|
{{ range .Site.Taxonomies.series.golang }}
|
2016-09-16 14:43:50 +00:00
|
|
|
|
<li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
|
2014-05-27 22:32:57 +00:00
|
|
|
|
{{ end }}
|
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
## 3. Listing 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.
|
|
|
|
|
|
|
|
|
|
### Example
|
|
|
|
|
|
|
|
|
|
<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>
|
|
|
|
|
|
2013-10-31 13:51:13 +00:00
|
|
|
|
|
2014-05-27 22:32:57 +00:00
|
|
|
|
## 4. Rendering a Site's Taxonomies
|
|
|
|
|
|
2015-01-29 19:34:56 +00:00
|
|
|
|
If you wish to display the list of all keys for a taxonomy, you can find retrieve
|
2014-01-11 02:19:19 +00:00
|
|
|
|
them from the `.Site` variable which is available on every page.
|
2013-10-31 13:51:13 +00:00
|
|
|
|
|
|
|
|
|
This may take the form of a tag cloud, a menu or simply a list.
|
|
|
|
|
|
|
|
|
|
The following example displays all tag keys:
|
|
|
|
|
|
2014-01-11 02:19:19 +00:00
|
|
|
|
### Example
|
2013-10-31 13:51:13 +00:00
|
|
|
|
|
2014-05-15 13:57:36 +00:00
|
|
|
|
<ul id="all-tags">
|
2014-08-30 03:39:55 +00:00
|
|
|
|
{{ range $name, $taxonomy := .Site.Taxonomies.tags }}
|
2016-05-14 04:35:16 +00:00
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
<li><a href="{{ "/tags/" | relLangURL }}{{ $name | urlize }}">{{ $name }}</a></li>
|
2014-05-15 13:57:36 +00:00
|
|
|
|
{{ end }}
|
|
|
|
|
</ul>
|
2013-10-31 13:51:13 +00:00
|
|
|
|
|
2014-05-27 22:32:57 +00:00
|
|
|
|
### Complete Example
|
|
|
|
|
This example will list all taxonomies, each of their keys and all the content assigned to each key.
|
2013-10-31 13:51:13 +00:00
|
|
|
|
|
2014-05-27 22:32:57 +00:00
|
|
|
|
<section>
|
2014-05-15 13:57:36 +00:00
|
|
|
|
<ul>
|
2014-05-27 22:32:57 +00:00
|
|
|
|
{{ range $taxonomyname, $taxonomy := .Site.Taxonomies }}
|
2016-05-14 04:35:16 +00:00
|
|
|
|
<li><a href="{{ "/" | relLangURL}}{{ $taxonomyname | urlize }}">{{ $taxonomyname }}</a>
|
2014-05-27 22:32:57 +00:00
|
|
|
|
<ul>
|
|
|
|
|
{{ range $key, $value := $taxonomy }}
|
2014-05-15 13:57:36 +00:00
|
|
|
|
<li> {{ $key }} </li>
|
|
|
|
|
<ul>
|
|
|
|
|
{{ range $value.Pages }}
|
|
|
|
|
<li hugo-nav="{{ .RelPermalink}}"><a href="{{ .Permalink}}"> {{ .LinkTitle }} </a> </li>
|
|
|
|
|
{{ end }}
|
|
|
|
|
</ul>
|
|
|
|
|
{{ end }}
|
|
|
|
|
</ul>
|
2014-10-07 23:52:58 +00:00
|
|
|
|
</li>
|
2014-05-15 13:57:36 +00:00
|
|
|
|
{{ end }}
|
|
|
|
|
</ul>
|
|
|
|
|
</section>
|