Resources
- Syntax
- PAGE.Resources
- Returns
- resource.Resources
Usage
The Resources method on a Page object returns a collection of the page resources owned by the given page, sorted by file path. Each element of the collection is a Resource object.
To work with global or remote resources, use the resources functions instead, as described in page, global, and remote resources.
Use these methods on the collection returned by the Resources method:
ByType- (
resource.Resources) Returns a collection of page resources of the given resource type, such asimage,text,audio,video,application, orpage. Returns an empty collection if none found. To get global resources by type, use theresources.ByTypefunction. Get- (
resource.Resource) Returns the page resource with the given path, relative to the page bundle, ornilif none found. The path is case-insensitive. To get a global resource, use theresources.Getfunction. GetMatch- (
resource.Resource) Returns the first page resource, sorted by file path, whose path matches the given glob pattern, ornilif none found. To get a global resource, use theresources.GetMatchfunction. Match- (
resource.Resources) Returns a collection of page resources whose paths match the given glob pattern, or an empty collection if none found. To get global resources, use theresources.Matchfunction. Mount- (
resource.ResourceGetter) Mounts the given resources, remapping from the base path (first argument) to the target path (second argument), and returns a resource getter. 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.
With the GetMatch and Match methods, Hugo determines a match using a case-insensitive glob pattern. Within a pattern, * matches any sequence of characters except the path separator, and ** matches any sequence of characters including the path separator. For example, images/* matches images/a.jpg but not images/sub/d.jpg, while images/** matches both. The glob patterns reference guide describes the other syntax rules.
If you set the name of a page resource in front matter, you can use the new name with the Get, GetMatch, and Match methods, as described in page resource metadata.
Examples
These examples use this content structure:
content/
└── posts/
└── post-1/
├── data/
│ └── books.json
├── images/
│ ├── a.jpg
│ ├── b.jpg
│ └── c.png
├── index.md
└── notes.mdUse the ByType method to render every image. With this content structure, the method returns images/a.jpg, images/b.jpg, and images/c.png:
{{ range .Resources.ByType "image" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}Rendering one image, using the Get method to get images/a.jpg:
{{ with .Resources.Get "images/a.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}Rendering the first JPEG image, using the GetMatch method to get images/a.jpg:
{{ with .Resources.GetMatch "images/*.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}Rendering every JPEG image, using the Match method to get images/a.jpg and images/b.jpg:
{{ range .Resources.Match "images/*.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}Mounting a directory of global resources relative to the page bundle, using the Mount method:
{{ $common := resources.Match "/js/headlessui/*.*" }}
{{ $importContext := (slice $.Page ($common.Mount "/js/headlessui" ".")) }}