HUGO
Menu
GitHub 90498 stars Mastodon

resources.Concat

Returns a resource created by concatenating the given slice of resources.
Syntax
resources.Concat TARGETPATH SLICE
Returns
resource.Resource

Usage

The resources.Concat function returns a resource created by concatenating the given slice of resources, caching the result using the target path as its cache key. Use it to bundle text resources such as JavaScript and CSS files. Each resource must have the same media type.

Hugo publishes the resource to the target path when you call its Publish, Permalink, or RelPermalink method, or pass it to the resources.Publish function.

New in v0.170.0

The SLICE argument now accepts a []any slice of resources, such as the slice returned by the [collections.Apply][] function.

Arguments

TARGETPATH
(string) The path of the published resource, relative to the site root.
SLICE
(resource.Resources or []any) A slice of the resources to concatenate, in order, such as the slice returned by the collections.Slice, collections.Apply, resources.ByType, or resources.Match function. Every element must be a resource. For example, a slice that contains a nil element, such as the value returned by the resources.Get function when it cannot find the file, causes an error.

Examples

{{ $plugins := resources.Get "js/plugins.js" }}
{{ $global := resources.Get "js/global.js" }}
{{ $js := slice $plugins $global | resources.Concat "js/bundle.js" }}

Creating the slice from a list of file paths:

{{ $paths := slice "js/plugins.js" "js/global.js" }}
{{ $resources := apply $paths "resources.Get" "." }}
{{ $js := resources.Concat "js/bundle.js" $resources }}

Creating the slice from a list of file paths, skipping files that do not exist:

{{ $paths := slice "js/plugins.js" "js/global.js" }}
{{ $resources := slice }}
{{ range $paths }}
  {{ with resources.Get . }}
    {{ $resources = $resources | append . }}
  {{ end }}
{{ end }}
{{ $js := resources.Concat "js/bundle.js" $resources }}