hugo/docs/content/indexes/displaying.md

3.1 KiB

title date linktitle groups groups_weight
Rendering Indexes 2013-07-01 Displaying
indexes
20

Rendering index values assigned to this content

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

Because we are leveraging the front matter system to define indexes for content, the indexes assigned to each content piece are located in the usual place (.Params.plural)

Example

{{% highlight html %}}

    {{ range .Params.tags }}
  • {{ . }}
  • {{ end }}
{{% /highlight %}}

Rendering a Site's Indexes

If you wish to display the list of all keys for an index you can find retrieve them from the .Site variable which is available on every page.

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

The following example displays all tag keys:

Example

{{% highlight html %}}

    {{ range .Site.Indexes.tags }}
  • {{ .Name }}
  • {{ end }}
{{% /highlight %}}

Creating a menu based on indexes

Hugo can generate menus based on indexes by iterating and nesting the index keys. This can be used to build a hierarchy of content within your site.

To have hugo create the menu, simply create a template in chrome called menu.html, then include it using the {{ template "chrome/menu.html" . }} syntax.

Example complete menu.html file

This example will list all indexes, each of their keys and all the content assigned to each key.

{{% highlight html %}}

    {{ range $indexname, $index := .Site.Indexes }}
  • {{ $indexname }}
      {{ range $key, $value := $index }}
    • {{ $key }}
      • {{ range $value.Pages }}
      • {{ .LinkTitle }}
      • {{ end }}
      {{ end }}
  • {{ end }}
{{% /highlight %}}

menu.html using a single index

It is more likely that you would want to use a single index for navigation. In this example we are using the groups index for our menu.

{{% highlight html %}}

    {{ range $key, $index := .Site.Indexes.groups }}
  • {{ $key }}
    • {{ range $index.Pages }}
    • {{ .LinkTitle }}
    • {{ end }}
    {{ end }}
{{% /highlight %}}

menu.html using a single index ordered by Popularity

{{% highlight html %}}

    {{ range .Site.Indexes.groups.ByCount }}
  • {{ .Name }}
    • {{ range .Pages }}
    • {{ .LinkTitle }}
    • {{ end }}
    {{ end }}
{{% /highlight %}}