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:
- The context for resolving relative paths (typically the current page).
- 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.jsonError handling
默認情況下,如果 Hugo 無法解析路徑,它將拋出錯誤並使構建失敗。您可以在站點配置中將其更改為警告,並指定在無法解析路徑時返回的 URL。
refLinksErrorLevel: warning
refLinksNotFoundURL: /some/other/url
refLinksErrorLevel = 'warning'
refLinksNotFoundURL = '/some/other/url'
{
"refLinksErrorLevel": "warning",
"refLinksNotFoundURL": "/some/other/url"
}