urls.RelRef
语法
urls.RelRef PAGE PATH
urls.RelRef PAGE OPTIONS
返回值
string
别名
relref
第一个参数是要从中解析相对路径的页面上下文,通常是当前页面。
第二个参数是页面的路径,可以带有或不带有文件扩展名,也可以带有或不带有锚点。不以 /
开头的路径首先相对于给定上下文解析,然后相对于站点的其余部分解析。或者,可以提供一个选项映射而不是路径。
{{ 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)
页面的路径,相对于content
目录。必需。- 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"
}