IsDefault
Syntax
Returns
The IsDefault method on a Site object reports whether the given site is the default site across all dimensions: language, version, and role. This is useful to ensure that a block of code executes only once per build, regardless of the number of sites generated by your dimensions.
For example, the following configuration defines a matrix of sites across language and version dimensions.
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: {}
[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']
{
"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": {}
}
}
If you call an initialization partial to handle one-time build logic or global variable setup, wrap that call in an if statement using this function. This prevents the logic from being executed for every dimensional variation.
{{ if .Site.IsDefault }}
{{ partial "init.html" . }}
{{ end }}In this setup, the code block is only executed for the English version v3.0.0 site. English is selected because it has the lowest weight, and version v3.0.0 is selected because it is the first version when sorted semantically in descending order.
