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