HUGO
Menu
GitHub 89546 stars Mastodon

hugo.Sites

Returns a collection of all sites for all dimensions.

Syntax

hugo.Sites

Returns

page.Sites
New in v0.156.0

The returned collection follows a hierarchical sort where each subsequent dimension acts as a tie-breaker for the one above it.

  1. Language is sorted by weight in ascending order, falling back to lexicographical order if weights are tied or undefined.
  2. Version is then sorted by weight in ascending order, with Hugo defaulting to a descending semantic sort for any ties.
  3. Role is finally sorted by weight in ascending order, using lexicographical order as the final fallback.

With this project configuration:

defaultContentLanguage: en
defaultContentLanguageInSubdir: true
defaultContentVersionInSubdir: true
languages:
  de:
    contentDir: content/de
    direction: ltr
    label: Deutsch
    locale: de-DE
    title: Projekt Dokumentation
    weight: 1
  en:
    contentDir: content/en
    direction: ltr
    label: English
    locale: en-US
    title: Project Documentation
    weight: 2
versions:
  v1.0.0: {}
  v2.0.0: {}
  v3.0.0: {}
defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true
defaultContentVersionInSubdir = true
[languages]
  [languages.de]
    contentDir = 'content/de'
    direction = 'ltr'
    label = 'Deutsch'
    locale = 'de-DE'
    title = 'Projekt Dokumentation'
    weight = 1
  [languages.en]
    contentDir = 'content/en'
    direction = 'ltr'
    label = 'English'
    locale = 'en-US'
    title = 'Project Documentation'
    weight = 2
[versions]
  [versions.'v1.0.0']
  [versions.'v2.0.0']
  [versions.'v3.0.0']
{
   "defaultContentLanguage": "en",
   "defaultContentLanguageInSubdir": true,
   "defaultContentVersionInSubdir": true,
   "languages": {
      "de": {
         "contentDir": "content/de",
         "direction": "ltr",
         "label": "Deutsch",
         "locale": "de-DE",
         "title": "Projekt Dokumentation",
         "weight": 1
      },
      "en": {
         "contentDir": "content/en",
         "direction": "ltr",
         "label": "English",
         "locale": "en-US",
         "title": "Project Documentation",
         "weight": 2
      }
   },
   "versions": {
      "v1.0.0": {},
      "v2.0.0": {},
      "v3.0.0": {}
   }
}

This template:

<ul>
  {{ range hugo.Sites }}
    <li><a href="{{ .Home.RelPermalink }}">{{ .Title }} {{ .Version.Name }}</a></li>
  {{ end }}
</ul>

Produces a list of links to each home page:

<ul>
  <li><a href="/v3.0.0/de/">Projekt Dokumentation v3.0.0</a></li>
  <li><a href="/v2.0.0/de/">Projekt Dokumentation v2.0.0</a></li>
  <li><a href="/v1.0.0/de/">Projekt Dokumentation v1.0.0</a></li>
  <li><a href="/v3.0.0/en/">Project Documentation v3.0.0</a></li>
  <li><a href="/v2.0.0/en/">Project Documentation v2.0.0</a></li>
  <li><a href="/v1.0.0/en/">Project Documentation v1.0.0</a></li>
</ul>

To render a link to the home page of the default site:

{{ with hugo.Sites.Default }}
  <a href="{{ .Home.RelPermalink }}">{{ .Title }}</a>
{{ end }}

With the configuration above, this renders a link to the home page of the English version v3.0.0 site. The default site is the site with the default language, default version, and default role, regardless of its position in the collection. In this example the three German sites appear first due to their lower language weight, but English is the default language per the defaultContentLanguage setting.