resources.Concat
- Syntax
- resources.Concat TARGETPATH SLICE
- Returns
- resource.Resource
This works with global resources, page resources, and remote resources.
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.Resourcesor[]any) A slice of the resources to concatenate, in order, such as the slice returned by thecollections.Slice,collections.Apply,resources.ByType, orresources.Matchfunction. Every element must be a resource. For example, a slice that contains anilelement, such as the value returned by theresources.Getfunction 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 }}