mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-20 21:31:32 +02:00
Merge commit '8b9803425e63e1b1801f8d5d676e96368d706722'
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: Name
|
||||
description: Returns the name of the given resource as optionally defined in front matter, falling back to a relative path or hashed file name depending on resource type.
|
||||
description: Returns the name of the given resource as optionally defined in front matter, falling back to its file path.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
@@ -20,51 +20,63 @@ With a [global resource], the `Name` method returns the path to the resource, re
|
||||
```text
|
||||
assets/
|
||||
└── images/
|
||||
└── a.jpg
|
||||
└── Sunrise in Bryce Canyon.jpg
|
||||
```
|
||||
|
||||
```go-html-template
|
||||
{{ with resources.Get "images/a.jpg" }}
|
||||
{{ .Name }} → images/a.jpg
|
||||
{{ with resources.Get "images/Sunrise in Bryce Canyon.jpg" }}
|
||||
{{ .Name }} → /images/Sunrise in Bryce Canyon.jpg
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
## Page resource
|
||||
|
||||
With a [page resource], the `Name` method returns the path to the resource, relative to the page bundle.
|
||||
With a [page resource], if you create an element in the `resources` array in front matter, the `Name` method returns the value of the `name` parameter.
|
||||
|
||||
```text
|
||||
content/
|
||||
├── posts/
|
||||
│ ├── post-1/
|
||||
│ │ ├── images/
|
||||
│ │ │ └── a.jpg
|
||||
│ │ └── index.md
|
||||
│ └── _index.md
|
||||
├── example/
|
||||
│ ├── images/
|
||||
│ │ └── a.jpg
|
||||
│ └── index.md
|
||||
└── _index.md
|
||||
```
|
||||
|
||||
{{< code-toggle file=content/example/index.md fm=true >}}
|
||||
title = 'Example'
|
||||
[[resources]]
|
||||
src = 'images/a.jpg'
|
||||
name = 'Sunrise in Bryce Canyon'
|
||||
{{< /code-toggle >}}
|
||||
|
||||
```go-html-template
|
||||
{{ with .Resources.Get "images/a.jpg" }}
|
||||
{{ .Name }} → Sunrise in Bryce Canyon
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
You can also capture the image by specifying its `name` instead of its path:
|
||||
|
||||
```go-html-template
|
||||
{{ with .Resources.Get "Sunrise in Bryce Canyon" }}
|
||||
{{ .Name }} → Sunrise in Bryce Canyon
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
If you do not create an element in the `resources` array in front matter, the `Name` method returns the file path, relative to the page bundle.
|
||||
|
||||
```text
|
||||
content/
|
||||
├── example/
|
||||
│ ├── images/
|
||||
│ │ └── Sunrise in Bryce Canyon.jpg
|
||||
│ └── index.md
|
||||
└── _index.md
|
||||
```
|
||||
|
||||
```go-html-template
|
||||
{{ with .Resources.Get "images/a.jpg" }}
|
||||
{{ .Name }} → images/a.jpg
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
If you create an element in the `resources` array in front matter, the `Name` method returns the value of the `name` parameter:
|
||||
|
||||
{{< code-toggle file=content/posts/post-1.md fm=true >}}
|
||||
title = 'Post 1'
|
||||
[[resources]]
|
||||
src = 'images/a.jpg'
|
||||
name = 'cat'
|
||||
title = 'Felix the cat'
|
||||
[resources.params]
|
||||
temperament = 'malicious'
|
||||
{{< /code-toggle >}}
|
||||
|
||||
```go-html-template
|
||||
{{ with .Resources.Get "cat" }}
|
||||
{{ .Name }} → cat
|
||||
{{ with .Resources.Get "images/Sunrise in Bryce Canyon.jpg" }}
|
||||
{{ .Name }} → images/Sunrise in Bryce Canyon.jpg
|
||||
{{ end }}
|
||||
```
|
||||
## Remote resource
|
||||
@@ -73,10 +85,11 @@ With a [remote resource], the `Name` method returns a hashed file name.
|
||||
|
||||
```go-html-template
|
||||
{{ with resources.GetRemote "https://example.org/images/a.jpg" }}
|
||||
{{ .Name }} → a_18432433023265451104.jpg
|
||||
{{ .Name }} → /a_18432433023265451104.jpg
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
[global resource]: /getting-started/glossary/#global-resource
|
||||
[logical path]: /getting-started/glossary/#logical-path
|
||||
[page resource]: /getting-started/glossary/#page-resource
|
||||
[remote resource]: /getting-started/glossary/#remote-resource
|
||||
|
Reference in New Issue
Block a user