Get relative resource from inside a resource

in my html head I’m:

{{ $components := slice }}
{{ range (resources.Match "components/*/*.js") }}
  {{ $c := . | resources.ExecuteAsTemplate "temp.js" . }}
  {{ $components = $components | append $c }}
{{ end }}
{{ $script := $components | resources.Concat "components.js"}}
<script src="{{- $script.RelPermalink -}}"></script>

the JavaScript needs to be executed as template because this components may need other resources. inside each js something like this may be found:

{{ with resources.Get "components/componentOne/style.css" }}

Question: How can I get rid of the absolute path for the resource and just get files next to the *.js in the folder? Keep in mind that this line is within a resource, not a Page

this does not work. But expresses what I’m looking for:

{{ with resources.Get "./style.css" }}

structure

assets/
└── components/
    ├── a/
    │   ├── a1.js
    │   ├── a2.js
    │   └── a-subresource.scss
    └── b/
        ├── b1.js
        ├── b2.js
        └── b-subresource.scss

layouts/partials/head.html

{{ $components := slice }}
{{ range resources.Match "components/*/*.js" }}
  {{ $c := . | resources.ExecuteAsTemplate "temp.js" . }}
  {{ $components = $components | append $c }}
{{ end }}
{{ $script := $components | resources.Concat "components.js" | minify }}
<script src="{{ $script.RelPermalink }}"></script>

assets/components/a/a1.js

{{- $base := "a-subresource.scss" -}}
{{- with $scss := resources.Get (path.Join (path.Dir .) $base) -}}
{{- $css := $scss | toCSS | minify -}}
const a1 = '{{ $css.Content }}';
{{- end -}}

jmooring, you are amazing, thaks for this solution and the clarity you manage to add to all of your answers, you already helped me a lot to understand HUGO better.

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