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 }}