HUGO
Menu
GitHub 86682 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 site configuration:

defaultContentLanguage: de
defaultContentLanguageInSubdir: true
defaultContentVersionInSubdir: true
languages:
  de:
    contentDir: content/de
    languageCode: de-DE
    languageDirection: ltr
    languageName: Deutsch
    title: Projekt Dokumentation
    weight: 1
  en:
    contentDir: content/en
    languageCode: en-US
    languageDirection: ltr
    languageName: English
    title: Project Documentation
    weight: 2
versions:
  v1.0.0: {}
  v2.0.0: {}
  v3.0.0: {}
defaultContentLanguage = 'de'
defaultContentLanguageInSubdir = true
defaultContentVersionInSubdir = true
[languages]
  [languages.de]
    contentDir = 'content/de'
    languageCode = 'de-DE'
    languageDirection = 'ltr'
    languageName = 'Deutsch'
    title = 'Projekt Dokumentation'
    weight = 1
  [languages.en]
    contentDir = 'content/en'
    languageCode = 'en-US'
    languageDirection = 'ltr'
    languageName = 'English'
    title = 'Project Documentation'
    weight = 2
[versions]
  [versions.'v1.0.0']
  [versions.'v2.0.0']
  [versions.'v3.0.0']
{
   "defaultContentLanguage": "de",
   "defaultContentLanguageInSubdir": true,
   "defaultContentVersionInSubdir": true,
   "languages": {
      "de": {
         "contentDir": "content/de",
         "languageCode": "de-DE",
         "languageDirection": "ltr",
         "languageName": "Deutsch",
         "title": "Projekt Dokumentation",
         "weight": 1
      },
      "en": {
         "contentDir": "content/en",
         "languageCode": "en-US",
         "languageDirection": "ltr",
         "languageName": "English",
         "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 }}

This is equivalent to:

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