hugo.Sites
- Syntax
- hugo.Sites
- Returns
- page.Sites
Usage
The hugo.Sites function returns a collection of all sites for all dimensions.
The returned collection follows a hierarchical sort where each subsequent dimension acts as a tie-breaker for the one above it.
- Language is sorted by weight in ascending order, falling back to lexicographical order if weights are tied or undefined.
- Version is then sorted by weight in ascending order, with Hugo defaulting to a descending semantic sort for any ties.
- Role is finally sorted by weight in ascending order, using lexicographical order as the final fallback.
Use the Default method on the returned collection to get the default site. The default site is the site with the default language, default version, and default role, regardless of its position in the collection.
Examples
These examples use 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": {}
}
}
Rendering a list of links to the home page of each site:
<ul>
{{ range hugo.Sites }}
<li><a href="{{ .Home.RelPermalink }}">{{ .Title }} {{ .Version.Name }}</a></li>
{{ end }}
</ul>The rendered output:
<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>Rendering a link to the home page of the default site:
{{ with hugo.Sites.Default }}
<a href="{{ .Home.RelPermalink }}">{{ .Title }}</a>
{{ end }}This renders a link to the home page of the English v3.0.0 site. The German sites appear first in the collection due to their lower language weight, but English is the default language as set by defaultContentLanguage.
