HUGO
Menu
GitHub 87548 stars Mastodon

Path

返回给定页面的逻辑路径。

Syntax

PAGE.Path

Returns

string

Page 对象上的 Path 方法返回给定页面的逻辑路径,无论页面是否有文件支持。

logical path(逻辑路径)是从文件路径派生的页面或页面资源标识符,不包括其扩展名和语言标识符。此值既不是文件路径也不?URL。从相对?content 目录的文件路径开始,Hugo 通过剥离文件扩展名和语言标识符、转换为小写、然后将空格替换为连字符来确定逻辑路径。路径段用斜杠(/)分隔

{{ .Path }} → /posts/post-1

从 2022 年 1 月发布的 v0.92.0 开始,Hugo 在调用 Path 方法时会发出警告。警告表明此方法将在未来版本中更改。

Page 对象上的 Path 方法的含义和返回值在 2024 年 2 月发布的 v0.123.0 中发生了变化。

Page 对象上的 Path 方法返回的值独立于内容格式、语言和 URL 修饰符,如 slugurl front matter 字段。

示例

单语站点

请注意,逻辑路径独立于内容格式和 URL 修饰符。

文件路径 Front matter 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 修饰符。

文件路径 Front matter 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 = /

查找页面

这些方法、函数和短代码使用逻辑路径查找给定页面:

使用任何这些方法、函数或短代码时指定逻辑路径。如果您包含文件扩展名或语言标识符,Hugo 会在逻辑树中查找页面之前剥离这些值。

逻辑树

正如文件路径形成文件树,逻辑路径形成逻辑树。

文件树:

content/
└── s1/
    ├── p1/
    │   └── index.md 
    └── p2.md

相同内容表示为逻辑树:

content/
└── s1/
    ├── p1
    └── p2

这些树之间的一个关键区别是从 p1 到 p2 的相对路径:

  • 在文件树中,从 p1 到 p2 的相对路径是 ../p2.md
  • 在逻辑树中,相对路径是 p2

使用上一节中列出的任何方法、函数或短代码时请记住使用逻辑路径。如果您包含文件扩展名或语言标识符,Hugo 会在逻辑树中查找页面之前剥离这些值。


Last updated: January 1, 0001
Improve this page