Resources
Syntax
Returns
The Resources
method on a Page
object returns a collection of page resources. A page resource is a file within a
page bundle.
To work with global or remote resources, see the
resources
functions.
Methods
ByType
(resource.Resources
) Returns a collection of page resources of the given
media type, or nil if none found. The media type is typically one of image
, text
, audio
, video
, or application
.
{{ range .Resources.ByType "image" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
When working with global resources instead of page resources, use the
resources.ByType
function.
Get
(resource.Resource
) Returns a page resource from the given path, or nil if none found.
{{ with .Resources.Get "images/a.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
When working with global resources instead of page resources, use the
resources.Get
function.
GetMatch
(resource.Resource
) Returns the first page resource from paths matching the given
glob pattern, or nil if none found.
{{ with .Resources.GetMatch "images/*.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
When working with global resources instead of page resources, use the
resources.GetMatch
function.
Match
(resource.Resources
) Returns a collection of page resources from paths matching the given
glob pattern, or nil if none found.
{{ range .Resources.Match "images/*.jpg" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
When working with global resources instead of page resources, use the
resources.Match
function.
Mount
New in v0.140.0(ResourceGetter
) Mounts the given resources from the two arguments base (string
) to the given target path (string
) and returns an object that implements
Get. Note that leading slashes in target marks an absolute path. Relative target paths allows you to mount resources relative to another set, e.g. a
Page bundle:
{{ $common := resources.Match "/js/headlessui/*.*" }}
{{ $importContext := (slice $.Page ($common.Mount "/js/headlessui" ".")) }}
This method is currently only useful in js.Batch.
Pattern matching
With the GetMatch
and Match
methods, Hugo determines a match using a case-insensitive
glob pattern.
Path | Pattern | Match |
---|---|---|
images/foo/a.jpg | images/foo/*.jpg | true |
images/foo/a.jpg | images/foo/*.* | true |
images/foo/a.jpg | images/foo/* | true |
images/foo/a.jpg | images/*/*.jpg | true |
images/foo/a.jpg | images/*/*.* | true |
images/foo/a.jpg | images/*/* | true |
images/foo/a.jpg | */*/*.jpg | true |
images/foo/a.jpg | */*/*.* | true |
images/foo/a.jpg | */*/* | true |
images/foo/a.jpg | **/*.jpg | true |
images/foo/a.jpg | **/*.* | true |
images/foo/a.jpg | **/* | true |
images/foo/a.jpg | ** | true |
images/foo/a.jpg | */*.jpg | false |
images/foo/a.jpg | *.jpg | false |
images/foo/a.jpg | *.* | false |
images/foo/a.jpg | * | false |