HUGO
Menu
GitHub 90454 stars Mastodon

urls.Ref

Returns the absolute URL of the page with the given path, language, and output format.
Syntax
urls.Ref PAGE PATH
urls.Ref PAGE OPTIONS
Returns
string
Alias
ref

Usage

The ref function takes two arguments:

  1. The context for resolving relative paths (typically the current page).
  2. Either the target page’s path or an options map (see below).

Options

path
(string) The logical path of the target page. The path resolution rules apply: Hugo resolves a path without a leading slash (/) first relative to the current page, then relative to the content directory. If the path does not contain a slash and Hugo has not found the page, Hugo searches for a page with the same base name, and fails the build if more than one page matches.
lang
(string) The language of the target page. Default is the current language. Optional.
outputFormat
(string) The output format of the target page. Default is the current output format. Optional.

Examples

The following examples show the rendered output for a page on the English version of the site:

{{ ref . "/books/book-1" }} → https://example.org/en/books/book-1/

{{ $opts := dict "path" "/books/book-1" }}
{{ ref . $opts }} → https://example.org/en/books/book-1/

{{ $opts := dict "path" "/books/book-1" "lang" "de" }}
{{ ref . $opts }} → https://example.org/de/books/book-1/

{{ $opts := dict "path" "/books/book-1" "lang" "de" "outputFormat" "json" }}
{{ ref . $opts }} → https://example.org/de/books/book-1/index.json

Error handling

By default, Hugo will throw an error and fail the build if it cannot resolve the logical path. You can change this to a warning in your project configuration, and specify a URL to return when the logical path cannot be resolved.

refLinksErrorLevel: warning
refLinksNotFoundURL: /some/other/url
refLinksErrorLevel = 'warning'
refLinksNotFoundURL = '/some/other/url'
{
   "refLinksErrorLevel": "warning",
   "refLinksNotFoundURL": "/some/other/url"
}