resources.GetMatch
- Syntax
- resources.GetMatch PATTERN
- Returns
- resource.Resource
Usage
The resources.GetMatch function returns the first global resource whose path matches the given glob pattern, or nil if none found. The path is relative to the assets directory.
This function operates on global resources. For page resources, use the corresponding Resources method on a Page object.
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.
Examples
Rendering the first file with a .jpg extension in the images directory:
{{ with resources.GetMatch "images/*.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}Rendering the first file with a .jpg extension in the images directory or its subdirectories:
{{ with resources.GetMatch "images/**.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}