路径
语法
PAGE.Path
返回
string
Page
对象上的 Path
方法返回给定页面的逻辑路径,而不管该页面是否有文件支持。
{{ .Path }} → /posts/post-1
此值既不是文件路径,也不是相对 URL。它是每个页面的逻辑标识符,独立于内容格式、语言和 URL 修改器。
为了确定由文件支持的页面的逻辑路径,Hugo 首先使用文件路径(相对于 content
目录),然后
- 删除文件扩展名
- 删除语言标识符
- 将结果转换为小写
- 用连字符替换空格
Page
对象上的 Path
方法返回的值独立于内容格式、语言和 URL 修改器,例如 slug
和 url
前言字段。
示例
单语站点
请注意,逻辑路径独立于内容格式和 URL 修改器。
文件路径 | 前言 slug | 逻辑路径 |
---|---|---|
content/_index.md |
/ |
|
content/posts/_index.md |
/posts |
|
content/posts/post-1.md |
foo |
/posts/post-1 |
content/posts/post-2.html |
bar |
/posts/post-2 |
多语言站点
请注意,逻辑路径独立于内容格式、语言标识符和 URL 修改器。
文件路径 | 前言 slug | 逻辑路径 |
---|---|---|
content/_index.en.md |
/ |
|
content/_index.de.md |
/ |
|
content/posts/_index.en.md |
/posts |
|
content/posts/_index.de.md |
/posts |
|
content/posts/posts-1.en.md |
foo |
/posts/post-1 |
content/posts/posts-1.de.md |
foo |
/posts/post-1 |
content/posts/posts-2.en.html |
bar |
/posts/post-2 |
content/posts/posts-2.de.html |
bar |
/posts/post-2 |
不以文件为后盾的页面
Page
对象上的 Path
方法返回值,而不管该页面是否有文件支持。
content/
└── posts/
└── post-1.md <-- front matter: tags = ['hugo']
当您构建站点时
public/
├── posts/
│ ├── post-1/
│ │ └── index.html .Page.Path = /posts/post-1
│ └── index.html .Page.Path = /posts
├── tags/
│ ├── hugo/
│ │ └── index.html .Page.Path = /tags/hugo
│ └── index.html .Page.Path = /tags
└── index.html .Page.Path = /
查找页面
以下方法、函数和短代码使用逻辑路径来查找给定页面
方法 | 函数 | 短代码 |
---|---|---|
Site.GetPage |
urls.Ref |
ref |
Page.GetPage |
urls.RelRef |
relref |
Page.Ref |
||
Page.RelRef |
||
Shortcode.Ref |
||
Shortcode.RelRef |
逻辑树
正如文件路径形成文件树一样,逻辑路径形成逻辑树。
文件树
content/
└── s1/
├── p1/
│ └── index.md
└── p2.md
同一内容表示为逻辑树
content/
└── s1/
├── p1
└── p2
这些树之间的主要区别在于从 p1 到 p2 的相对路径
- 在文件树中,从 p1 到 p2 的相对路径是
../p2.md
- 在逻辑树中,相对路径是
p2