Path

New in v0.123.0

Page 對象的 Path 方法返回給定頁面的邏輯路徑,無論該頁面是否由文件支持。

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

此值既不是文件路徑,也不是相對 URL。它是每個頁面的邏輯標識符,與內容格式、語言和 URL 修飾符無關。

Hugo 通過以下步驟來確定基於文件的頁面邏輯路徑,從相對於內容目錄的文件路徑開始:

  1. 刪除文件擴展名
  2. 刪除語言標識符
  3. 將結果轉換為小寫
  4. 用連字符替換空格

Path 方法返回的值與內容格式、語言標識符和 URL 修飾符(如 front matter 中的 slugurl)無關。

示例

單語言網站

請注意,邏輯路徑不受內容格式和 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 = /

查找頁面

這些方法、函數和短代碼使用邏輯路徑來查找指定頁面:

方法 函數 短代碼
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