HUGO
Menu
GitHub 90479 stars Mastodon

site

Returns the Site object of the site being rendered, accessible from any context.
Syntax
site
Returns
page.siteWrapper

Usage

The site function returns the Site object of the site being rendered, accessible from any context.

To simplify your templates, use the global site function regardless of whether the current Site object is in context.

A partial template receives only the data passed to it. With the global site function, you can access the Site object within a partial template without passing the Page object:

layouts/page.html
{{ partial "foo.html" (dict "color" "red") }}
layouts/_partials/foo.html
{{ site.Title }}
{{ .color }}

Examples

This example shows a common pitfall and how to avoid it.

With this project configuration:

languages:
  de:
    title: Projekt Dokumentation
    weight: 1
  en:
    title: Project Documentation
    weight: 2
[languages]
  [languages.de]
    title = 'Projekt Dokumentation'
    weight = 1
  [languages.en]
    title = 'Project Documentation'
    weight = 2
{
   "languages": {
      "de": {
         "title": "Projekt Dokumentation",
         "weight": 1
      },
      "en": {
         "title": "Project Documentation",
         "weight": 2
      }
   }
}

And this code in the home template:

layouts/home.html
{{ range hugo.Sites }}
  {{ site.Title }}
{{ end }}

The rendered output of the English home page is:

Project Documentation
Project Documentation

In the example above, the global site function accesses the Site object of the site being rendered, not the Site object of the iterated sites. Use the dot to access the iterated sites:

layouts/home.html
{{ range hugo.Sites }}
  {{ .Title }}
{{ end }}