| 1 |
--- |
| 2 |
title: Name |
| 3 |
description: Returns the name of the given resource as optionally defined in front matter, falling back to its file path. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: string |
| 9 |
signatures: [RESOURCE.Name] |
| 10 |
--- |
| 11 |
|
| 12 |
The value returned by the `Name` method on a `Resource` object depends on the resource type. |
| 13 |
|
| 14 |
## Global resource |
| 15 |
|
| 16 |
With a [global resource](g), the `Name` method returns the path to the resource, relative to the `assets` directory. |
| 17 |
|
| 18 |
```tree |
| 19 |
assets/ |
| 20 |
└── images/ |
| 21 |
└── Sunrise in Bryce Canyon.jpg |
| 22 |
``` |
| 23 |
|
| 24 |
```go-html-template |
| 25 |
{{ with resources.Get "images/Sunrise in Bryce Canyon.jpg" }} |
| 26 |
{{ .Name }} → /images/Sunrise in Bryce Canyon.jpg |
| 27 |
{{ end }} |
| 28 |
``` |
| 29 |
|
| 30 |
## Page resource |
| 31 |
|
| 32 |
With a [page resource](g), if you create an element in the `resources` array in front matter, the `Name` method returns the value of the `name` parameter. |
| 33 |
|
| 34 |
```tree |
| 35 |
content/ |
| 36 |
├── example/ |
| 37 |
│ ├── images/ |
| 38 |
│ │ └── a.jpg |
| 39 |
│ └── index.md |
| 40 |
└── _index.md |
| 41 |
``` |
| 42 |
|
| 43 |
{{< code-toggle file=content/example/index.md fm=true >}} |
| 44 |
title = 'Example' |
| 45 |
[[resources]] |
| 46 |
src = 'images/a.jpg' |
| 47 |
name = 'Sunrise in Bryce Canyon' |
| 48 |
{{< /code-toggle >}} |
| 49 |
|
| 50 |
```go-html-template |
| 51 |
{{ with .Resources.Get "images/a.jpg" }} |
| 52 |
{{ .Name }} → Sunrise in Bryce Canyon |
| 53 |
{{ end }} |
| 54 |
``` |
| 55 |
|
| 56 |
You can also capture the image by specifying its `name` instead of its path: |
| 57 |
|
| 58 |
```go-html-template |
| 59 |
{{ with .Resources.Get "Sunrise in Bryce Canyon" }} |
| 60 |
{{ .Name }} → Sunrise in Bryce Canyon |
| 61 |
{{ end }} |
| 62 |
``` |
| 63 |
|
| 64 |
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. |
| 65 |
|
| 66 |
```tree |
| 67 |
content/ |
| 68 |
├── example/ |
| 69 |
│ ├── images/ |
| 70 |
│ │ └── Sunrise in Bryce Canyon.jpg |
| 71 |
│ └── index.md |
| 72 |
└── _index.md |
| 73 |
``` |
| 74 |
|
| 75 |
```go-html-template |
| 76 |
{{ with .Resources.Get "images/Sunrise in Bryce Canyon.jpg" }} |
| 77 |
{{ .Name }} → images/Sunrise in Bryce Canyon.jpg |
| 78 |
{{ end }} |
| 79 |
``` |
| 80 |
|
| 81 |
## Remote resource |
| 82 |
|
| 83 |
With a [remote resource](g), the `Name` method returns a hashed file name. |
| 84 |
|
| 85 |
```go-html-template |
| 86 |
{{ with resources.GetRemote "https://example.org/images/a.jpg" }} |
| 87 |
{{ .Name }} → /a_18432433023265451104.jpg |
| 88 |
{{ end }} |
| 89 |
``` |