名称
语法
RESOURCE.Name
返回值
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 }}
页面资源
对于 页面资源,如果在前言的 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 }}