名称
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
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 }}
如果您没有在 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 }}