Skip to content

Commit 9ba1e15

Browse files
committed
content: Add Site.IsDefault
1 parent 5e29138 commit 9ba1e15

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
## Use case
13+
14+
This method is useful to ensure that a block of code executes only once per [project](g) build, regardless of the number of [sites](g) generated by your [dimensions](g).
15+
16+
## Default site identification
17+
18+
The default site occupies the primary position within your multidimensional content matrix. Hugo identifies this site by selecting the first value from each dimension based on the following priority.
19+
20+
1. [Language](g) by the lowest [weight](g) or by lexicographical order if weights are tied or undefined.
21+
1. [Role](g) by the lowest weight or by lexicographical order if weights are tied or undefined.
22+
1. [Version](g) by the lowest weight or by the first version when sorted semantically in descending order if weights are tied or undefined.
23+
24+
## Example
25+
26+
If you are generating a global search index or a single sitemap for a project containing multiple languages and versions, wrap that logic in an [`if`][] statement using this function. This prevents the index from being overwritten or redundantly processed for every dimensional variation.
27+
28+
[`if`]: /functions/go-template/if/
29+
30+
The following configuration defines a matrix of sites across language and version dimensions.
31+
32+
{{< code-toggle file=hugo >}}
33+
[languages.en]
34+
languageCode = 'en-US'
35+
languageDirection = 'ltr'
36+
languageName = 'English'
37+
weight = 1
38+
39+
[languages.de]
40+
languageCode = 'de-DE'
41+
languageDirection = 'ltr'
42+
languageName = 'Deutsch'
43+
weight = 3
44+
45+
[versions.'v1.0.0']
46+
[versions.'v2.0.0']
47+
[versions.'v3.0.0']
48+
{{< /code-toggle >}}
49+
50+
In your templates, use the following logic to restrict code execution to the default site only.
51+
52+
```go-html-template
53+
{{ if hugo.IsDefault }}
54+
...
55+
{{ end }}
56+
```
57+
58+
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.

0 commit comments

Comments
 (0)