Initialize empty variable/array for resources

Hi,
I’m creating an assets bundle. I have multiple folders with sass files (“components”, “forms”, “buttons” etc). I’m iterating through all those folders and adding files from each folder to “main.scss” file. Code below works but I would like to remove the need to Initialize $style variable with a resource.

{{ $style := resources.Get "scss/main.scss" }}

I would like to initialize $style as an empty array to which I later add all those files.

Something like this (it does not work)

{{ $style := [] }}

My current code:

{{ $style := resources.Get "scss/main.scss" }}
{{ $components := resources.Match "scss/**/*.scss" }}
{{ range $components }}
    {{ $style = slice $style . | resources.Concat .Name  }}
{{ end }}
{{ $style = slice $style | resources.Concat "main.scss" | toCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $style.Permalink }}">

Try {{ $style := slice }}

Unfortunately it does not work. It gives this error:

at <resources.Concat>: error calling Concat: slice []interface {} not supported in concat

You will have to modify the rest of your code as well, since $style is now an array.

Something like

{{ $stylearr := slice }}

{{ range $components }}
  {{ $stylearr = $stylearr | append . }}
{{ end }}

{{ $generatedstyle := $stylearr | resources.Concat "main.scss" | toCSS $options | minify | fingerprint }}
{{ $generatedstyle.Permalink }}
1 Like

It worked and this makes so much sense now.

Thanks and have a good one!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.