HUGO
Menu
GitHub 87548 stars Mastodon

urls.RelRef

Returns the relative URL of the page with the given path, language, and output format.

Syntax

urls.RelRef PAGE PATH
urls.RelRef PAGE OPTIONS

Returns

string

Alias

relref

Usage

The relref 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) 目标页面的路径。没有前导斜杠 (/) 的路径首先相对于当前页面解析,然后相对于站点其余部分解析。
lang
(string) 目标页面的语言。默认为当前语言。可选。
outputFormat
(string) 目标页面的输出格式。默认为当前输出格式。可选。

Examples

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

{{ relref . "/books/book-1" }} → /zh/books/book-1/

{{ $opts := dict "path" "/books/book-1" }}
{{ relref . $opts }} → /zh/books/book-1/

{{ $opts := dict "path" "/books/book-1" "lang" "de" }}
{{ relref . $opts }} → /de/books/book-1/

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

Error handling

默认情况下,如果 Hugo 无法解析路径,它将抛出错误并使构建失败。您可以在站点配置中将其更改为警告,并指定在无法解析路径时返回的 URL。

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

Last updated: January 1, 0001
Improve this page