HUGO
Menu
GitHub 90605 stars Mastodon

resources.ByType

Returns a collection of global resources of the given resource type, or nil if none found.
Syntax
resources.ByType TYPE
Returns
resource.Resources

Usage

The resources.ByType function returns a collection of global resources of the given resource type, or nil if none found. The resource type is the main type of a resource’s media type, typically one of image, text, audio, video, or application. For example, the resource type of a file with media type image/jpeg is image.

This function operates on global resources. For page resources, use the corresponding Resources method on a Page object.

Examples

This example renders an img element for each image. Hugo cannot determine the dimensions of some image formats, such as SVG, so the example uses the reflect.IsImageResourceWithMeta function to include the width and height attributes only when the dimensions are available:

{{ range resources.ByType "image" }}
  {{ if reflect.IsImageResourceWithMeta . }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ else }}
    <img src="{{ .RelPermalink }}" alt="">
  {{ end }}
{{ end }}