| 1 |
--- |
| 2 |
title: IsDefault |
| 3 |
description: Reports whether the given site is the default site across all dimensions. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: bool |
| 9 |
signatures: [SITE.IsDefault] |
| 10 |
--- |
| 11 |
|
| 12 |
{{< new-in 0.156.0 />}} |
| 13 |
|
| 14 |
The `IsDefault` method on a `Site` object reports whether the given site is the [default site](g) across all dimensions: [language](g), [version](g), and [role](g). This is useful to ensure that a block of code executes only once per build, regardless of the number of [sites](g) generated by your [dimensions](g). |
| 15 |
|
| 16 |
For example, the following configuration defines a matrix of sites across language and version dimensions. |
| 17 |
|
| 18 |
{{< code-toggle file=hugo >}} |
| 19 |
[languages.de] |
| 20 |
contentDir = 'content/de' |
| 21 |
direction = 'ltr' |
| 22 |
label = 'Deutsch' |
| 23 |
locale = 'de-DE' |
| 24 |
title = 'Projekt Dokumentation' |
| 25 |
weight = 1 |
| 26 |
|
| 27 |
[languages.en] |
| 28 |
contentDir = 'content/en' |
| 29 |
direction = 'ltr' |
| 30 |
label = 'English' |
| 31 |
locale = 'en-US' |
| 32 |
title = 'Project Documentation' |
| 33 |
weight = 2 |
| 34 |
|
| 35 |
[versions.'v1.0.0'] |
| 36 |
[versions.'v2.0.0'] |
| 37 |
[versions.'v3.0.0'] |
| 38 |
{{< /code-toggle >}} |
| 39 |
|
| 40 |
If you call an initialization _partial_ template 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. |
| 41 |
|
| 42 |
```go-html-template |
| 43 |
{{ if .Site.IsDefault }} |
| 44 |
{{ partial "init.html" . }} |
| 45 |
{{ end }} |
| 46 |
``` |
| 47 |
|
| 48 |
In this setup, the code block is only executed for the English version v3.0.0 site. English is selected because the [`defaultContentLanguage`][] setting is not defined, making English the [default language](g). Version v3.0.0 is selected because the [`defaultContentVersion`][] setting is not defined, making v3.0.0 the [default version](g). |
| 49 |
|
| 50 |
[`defaultContentLanguage`]: /configuration/all/#defaultcontentlanguage |
| 51 |
[`defaultContentVersion`]: /configuration/all/#defaultcontentversion |
| 52 |
[`if`]: /functions/go-template/if/ |