路径
Syntax
PAGE.Path
Returns
string
Page
对象上的 Path
方法返回给定页面的逻辑路径,无论该页面是否由文件支持。
{{ .Path }} → /posts/post-1
此值既不是文件路径也不是相对 URL。它是每个页面的逻辑标识符,独立于内容格式、语言和 URL 修饰符。
为了确定由文件支持的页面的逻辑路径,Hugo 从相对于内容目录的文件路径开始,然后:
- 去除文件扩展名
- 去除语言标识符
- 将结果转换为小写
- 将空格替换为连字符
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