urls.RelRef
第一個參數是解析相對路徑的頁面上下文,通常為當前頁面。
第二個參數是頁面的路徑,可以有或沒有副檔名,也可以有或沒有錨點(anchor)。若路徑未以 /
開頭,將先相對於給定上下文解析,然後解析至整個網站。或者,您也可以提供一個選項映射來替代路徑。
{{ relref . "about" }}
{{ relref . "about#anchor" }}
{{ relref . "about.md" }}
{{ relref . "about.md#anchor" }}
{{ relref . "#anchor" }}
{{ relref . "/blog/my-post" }}
{{ relref . "/blog/my-post.md" }}
返回的永久連結相對於網站配置中 baseURL
的協議+主機部分。例如:
代碼 | baseURL | 永久連結 |
---|---|---|
{{ relref . "/about" }} |
https://example.org/ |
/about/ |
{{ relref . "/about" }} |
https://example.org/x/ |
/x/about/ |
選項
除了指定路徑,您還可以提供一個選項映射:
- path
- (
string
) 頁面路徑,相對於內容目錄。必填。 - lang
- (
string
) 搜尋頁面的語言(網站)。預設為當前語言。選填。 - outputFormat
- (
string
) 搜尋頁面的輸出格式。預設為當前輸出格式。選填。
若要返回頁面其他語言版本的相對永久連結:
{{ relref . (dict "path" "about.md" "lang" "fr") }}
若要返回頁面其他輸出格式的相對永久連結:
{{ relref . (dict "path" "about.md" "outputFormat" "rss") }}
預設情況下,若無法解析路徑,Hugo 將拋出錯誤並終止建構。您可以在網站配置中將其改為警告,並指定一個 URL 作為無法解析時的返回值。
hugo.
refLinksErrorLevel: warning
refLinksNotFoundURL: /some/other/url
refLinksErrorLevel = 'warning'
refLinksNotFoundURL = '/some/other/url'
{
"refLinksErrorLevel": "warning",
"refLinksNotFoundURL": "/some/other/url"
}