Name
返回給定資源的名稱,如 front matter 中所定義,或回退到其文件路徑。
Syntax
RESOURCE.Name
Returns
string
Resource 對象上的 Name 方法返回的值取決於資源類型。
全局資源
使用 全局資源 時,Name 方法返回資源的路徑,相對於 assets 目錄。
assets/
└── images/
└── Sunrise in Bryce Canyon.jpg{{ with resources.Get "images/Sunrise in Bryce Canyon.jpg" }}
{{ .Name }} → /images/Sunrise in Bryce Canyon.jpg
{{ end }}頁面資源
使用 頁面資源 時,如果在 front matter 的 resources 數組中創建了一個元素,Name 方法返回 name 參數的值。
content/
├── example/
│ ├── images/
│ │ └── a.jpg
│ └── index.md
└── _index.md---
resources:
- name: Sunrise in Bryce Canyon
src: images/a.jpg
title: Example
---+++
title = 'Example'
[[resources]]
name = 'Sunrise in Bryce Canyon'
src = 'images/a.jpg'
+++{
"resources": [
{
"name": "Sunrise in Bryce Canyon",
"src": "images/a.jpg"
}
],
"title": "Example"
}
{{ with .Resources.Get "images/a.jpg" }}
{{ .Name }} → Sunrise in Bryce Canyon
{{ end }}您也可以通過指定其 name 而不是路徑來捕獲圖像:
{{ with .Resources.Get "Sunrise in Bryce Canyon" }}
{{ .Name }} → Sunrise in Bryce Canyon
{{ end }}如果您不在 front matter 的 resources 數組中創建元素,Name 方法返回文件路徑,相對於頁面包。
content/
├── example/
│ ├── images/
│ │ └── Sunrise in Bryce Canyon.jpg
│ └── index.md
└── _index.md{{ with .Resources.Get "images/Sunrise in Bryce Canyon.jpg" }}
{{ .Name }} → images/Sunrise in Bryce Canyon.jpg
{{ end }}遠程資源
使用 遠程資源 時,Name 方法返回哈希文件名。
{{ with resources.GetRemote "https://example.org/images/a.jpg" }}
{{ .Name }} → /a_18432433023265451104.jpg
{{ end }}