HUGO
Menu
GitHub 87548 stars Mastodon

File

對於有文件支持的頁面,返回給定頁面的文件信息。

Syntax

PAGE.File

Returns

hugolib.fileInfo

默認情況下,並非所有頁面都有文件支持,包括頂層 欄目頁面分類法頁面術語頁面。根據定義,當文件不存在時,您無法檢索文件信息。

要為上述頁面之一提供文件支持,請在相應目錄中創建 _index.md 文件。例如:

content/
└── books/
    ├── _index.md  <-- 頂層欄目頁面
    ├── book-1.md
    └── book-2.md

通過如下示例中所示驗證文件存在性來防御性編碼。

方法

PathDirFilename 中的路徑分隔符(斜槓或反斜槓)取決於操作系統。

BaseFileName

(string) 文件名,不包括擴展名。

{{ with .File }}
  {{ .BaseFileName }}
{{ end }}

ContentBaseName

(string) 如果頁面是分支或葉捆綁包,則為包含目錄的名稱,否則為 TranslationBaseName

{{ with .File }}
  {{ .ContentBaseName }}
{{ end }}

Dir

(string) 文件路徑,不包括文件名,相對於 content 目錄。

{{ with .File }}
  {{ .Dir }}
{{ end }}

Ext

(string) 文件擴展名。

{{ with .File }}
  {{ .Ext }}
{{ end }}

Filename

(string) 絕對文件路徑。

{{ with .File }}
  {{ .Filename }}
{{ end }}

IsContentAdapter

New in v0.126.0

(bool) 報告文件是否為 內容適配器

{{ with .File }}
  {{ .IsContentAdapter }}
{{ end }}

LogicalName

(string) 文件名。

{{ with .File }}
  {{ .LogicalName }}
{{ end }}

Path

(string) 文件路徑,相對於 content 目錄。

{{ with .File }}
  {{ .Path }}
{{ end }}

Section

(string) 文件所在頂層欄目的名稱。

{{ with .File }}
  {{ .Section }}
{{ end }}

TranslationBaseName

(string) 文件名,不包括擴展名和語言標識符。

{{ with .File }}
  {{ .TranslationBaseName }}
{{ end }}

UniqueID

(string) .File.Path 的 MD5 哈希值。

{{ with .File }}
  {{ .UniqueID }}
{{ end }}

示例

考慮多語言項目中的此內容結構:

content/
├── news/
│   ├── b/
│   │   ├── index.de.md   <-- 葉捆綁包
│   │   └── index.en.md   <-- 葉捆綁包
│   ├── a.de.md           <-- 常規內容
│   ├── a.en.md           <-- 常規內容
│   ├── _index.de.md      <-- 分支捆綁包
│   └── _index.en.md      <-- 分支捆綁包
├── _index.de.md
└── _index.en.md

使用英語語言站點:

  常規內容 葉捆綁包 分支捆綁包
BaseFileName a.en index.en _index.en
ContentBaseName a b news
Dir news/ news/b/ news/
Ext md md md
Filename /home/user/… /home/user/… /home/user/…
IsContentAdapter false false false
LogicalName a.en.md index.en.md _index.en.md
Path news/a.en.md news/b/index.en.md news/_index.en.md
Section news news news
TranslationBaseName a index _index
UniqueID 15be14b… 186868f… 7d9159d…

防御性編碼

站點上的某些頁面可能沒有文件支持。例如:

  • 頂層欄目頁面
  • 分類法頁面
  • 術語頁面

沒有支持文件,如果您嘗試訪問 .File 屬性,Hugo 將拋出錯誤。要防御性編碼,首先檢查文件存在性:

{{ with .File }}
  {{ .ContentBaseName }}
{{ end }}

Last updated: January 1, 0001
Improve this page