HUGO
Menu
GitHub 86668 stars Mastodon

IsDefault

Reports whether the given site is the default site across all dimensions.

Syntax

SITE.IsDefault

Returns

bool
New in v0.156.0

Use case

This method is useful to ensure that a block of code executes only once per project build, regardless of the number of sites generated by your dimensions.

Default site identification

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.

  1. Language by the lowest weight or by lexicographical order if weights are tied or undefined.
  2. Role by the lowest weight or by lexicographical order if weights are tied or undefined.
  3. Version by the lowest weight or by the first version when sorted semantically in descending order if weights are tied or undefined.

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.