How to Reference Information Set in Hugo Configuration Files (config.yml)

This article explains how to reference information configured in Hugo configuration files such as config.yml.

Configuration File Basics

General settings for a Hugo site are placed in the root directory and use one of the following configuration files. If multiple files exist, the first discovered one is selected.

File name Description
config.toml Written in TOML format (default)
config.yaml Written in YAML format
config.json Written in JSON format

Example config.toml

baseURL = "https://www.devkuam.com/"
languageCode = "ko-kr"
title = "My Blog"
themesDir = "../hugo_themes"
theme = "devkuma"

Configurable parameters and their default values are listed below.

Configure Hugo | Hugo

Referencing Setting Values

Referencing Parameters Defined by Hugo

Parameters set in the configuration file can be referenced in template files using the .Site variable. For example, values such as the title parameter and baseURL set in config.toml can be referenced as follows.

This site's title is <b>{{ $.Site.Title }}</b>.
This site's base URL is <b>{{ $.Site.BaseURL }}</b>.

Parameter names begin with uppercase letters, so use .Site.Title rather than .Site.title. You can see which property names correspond to each setting by checking the list of Site variables below.

Site Variables | Hugo

To branch processing depending on whether a parameter is configured, write it as follows.

{{ with $.Site.GoogleAnalytics }}
  The Google Analytics tracking ID is <b>{{ . }}</b>.
{{ else }}
  The Google Analytics tracking ID is not configured.
{{ end }}

The example above uses the googleAnalytics parameter to check whether a tracking ID for Google Analytics has been configured.

Referencing Custom Parameters

To set custom parameters in the configuration file, add parameters under the params section.

config.toml

baseURL = "https://www.devkuam.com/"
languageCode = "ko-kr"
title = "My Blog"

[params]
  GitHubUser = "redfreek2c"
  Twitter = "kimkc"
  Subtitle = "Technical sharing"
  Description = "This section records detailed site information"
  SidebarRecentLimit = 5
  ListOfFoo = ["foo1", "foo2"]

Custom parameters can be referenced from template files in the form $.Site.Params.name.

The example below references the Description value of a custom parameter variable.
layouts/partials/head.html

<meta name="description" content="{{ if .IsHome }}{{ $.Site.Params.description }}{{ else }}{{ .Description }}{{ end }}" />

Here, the main first page (home page) references the configuration file setting ($.Site.Params.Description), while other pages reference the description value set in each page’s front matter.