Name
Name
方法
Resource
物件上的 Name
方法返回的值依賴於資源類型。
全域資源
對於 全域資源,Name
方法會返回相對於資源目錄的資源路徑。
assets/
└── images/
└── Sunrise in Bryce Canyon.jpg
{{ with resources.Get "images/Sunrise in Bryce Canyon.jpg" }}
{{ .Name }} → /images/Sunrise in Bryce Canyon.jpg
{{ end }}
頁面資源
對於 頁面資源,如果你在前置資料中的 resources
陣列內建立了元素,則 Name
方法會返回 name
參數的值。
content/
├── example/
│ ├── images/
│ │ └── a.jpg
│ └── index.md
└── _index.md
content/example/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 }}
如果你在前置資料的 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 }}