| 1 |
--- |
| 2 |
title: Resources |
| 3 |
description: Returns a collection of page resources. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: resource.Resources |
| 9 |
signatures: [PAGE.Resources] |
| 10 |
--- |
| 11 |
|
| 12 |
The `Resources` method on a `Page` object returns a collection of page resources. A page resource is a file within a [page bundle](g). |
| 13 |
|
| 14 |
To work with global or remote resources, see the [`resources`][] functions. |
| 15 |
|
| 16 |
## Methods |
| 17 |
|
| 18 |
Use these methods on the `Resources` object. |
| 19 |
|
| 20 |
`ByType` |
| 21 |
: (`resource.Resources`) Returns a collection of page resources of the given [media type](g), or `nil` if none found. The media type is typically one of `image`, `text`, `audio`, `video`, or `application`. |
| 22 |
|
| 23 |
```go-html-template |
| 24 |
{{ range .Resources.ByType "image" }} |
| 25 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 26 |
{{ end }} |
| 27 |
``` |
| 28 |
|
| 29 |
When working with global resources instead of page resources, use the [`resources.ByType`][] function. |
| 30 |
|
| 31 |
`Get` |
| 32 |
: (`resource.Resource`) Returns a page resource from the given path, or `nil` if none found. |
| 33 |
|
| 34 |
```go-html-template |
| 35 |
{{ with .Resources.Get "images/a.jpg" }} |
| 36 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 37 |
{{ end }} |
| 38 |
``` |
| 39 |
|
| 40 |
When working with global resources instead of page resources, use the [`resources.Get`][] function. |
| 41 |
|
| 42 |
`GetMatch` |
| 43 |
: (`resource.Resource`) Returns the first page resource from paths matching the given [glob pattern](g), or `nil` if none found. |
| 44 |
|
| 45 |
```go-html-template |
| 46 |
{{ with .Resources.GetMatch "images/*.jpg" }} |
| 47 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 48 |
{{ end }} |
| 49 |
``` |
| 50 |
|
| 51 |
When working with global resources instead of page resources, use the [`resources.GetMatch`][] function. |
| 52 |
|
| 53 |
`Match` |
| 54 |
: (`resource.Resources`) Returns a collection of page resources from paths matching the given [glob pattern](g), or `nil` if none found. |
| 55 |
|
| 56 |
```go-html-template |
| 57 |
{{ range .Resources.Match "images/*.jpg" }} |
| 58 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 59 |
{{ end }} |
| 60 |
``` |
| 61 |
|
| 62 |
When working with global resources instead of page resources, use the [`resources.Match`][] function. |
| 63 |
|
| 64 |
`Mount` |
| 65 |
: {{< new-in 0.140.0 />}} |
| 66 |
: (`resource.ResourceGetter`) Mounts the given resources, remapping from the base path (first argument) to the target path (second argument), and returns a [resource getter](g). A leading slash in the target marks an absolute path. Relative target paths allow you to mount resources relative to another set, such as a [page bundle](g): |
| 67 |
|
| 68 |
```go-html-template |
| 69 |
{{ $common := resources.Match "/js/headlessui/*.*" }} |
| 70 |
{{ $importContext := (slice $.Page ($common.Mount "/js/headlessui" ".")) }} |
| 71 |
``` |
| 72 |
|
| 73 |
## Pattern matching |
| 74 |
|
| 75 |
With the `GetMatch` and `Match` methods, Hugo determines a match using a case-insensitive [glob pattern](g). |
| 76 |
|
| 77 |
{{% include "/_common/glob-patterns.md" %}} |
| 78 |
|
| 79 |
[`resources.ByType`]: /functions/resources/bytype/ |
| 80 |
[`resources.GetMatch`]: /functions/resources/getmatch/ |
| 81 |
[`resources.Get`]: /functions/resources/get/ |
| 82 |
[`resources.Match`]: /functions/resources/match/ |
| 83 |
[`resources`]: /functions/resources/ |