resources.Get from dynamic file name

I am trying to inline css based on the id of the page which I determine per page based on the section etc.

{{ $id := “home” }}
{{ $scss := printf “/scss/main/%s.scss” $id -}}

This works fine:

{{ with resources.Get “/scss/main/home.scss” | toCSS | minify }}
{{ .Content | safeCSS }}
{{ end }}

This also works fine though why the need for the if-file-exists? Without it I get “error calling toCSS: type not supported in Resource transformations”.

{{ if (fileExists ( printf “assets/%s” $scss )) }}
{{ with resources.Get $scss | toCSS | minify}}
{{ .Content }}
{{end}}
{{ end }}

If I add the style tags it gives an error over context:

{{ if (fileExists ( printf “assets/%s” $scss )) }}
{{ with resources.Get $scss | toCSS | minify}}
{{ .Content }}
{{end}}
{{ end }}

{{with}} branches end in different contexts: {stateCSS delimNone urlPartNone jsCtxRegexp attrNone elementStyle }, {stateText delimNone urlPartNone jsCtxRegexp attrNone elementNone }

I suspect I am still doing something not right.

I use v0.68.3

Unable to reproduce with 0.73. Didn’t try earlier versions.

layouts/_default/baseof.html:

{{- $id := "home" -}}
{{- $scss := printf "scss/main/%s.scss" $id -}}
{{- with resources.Get $scss | toCSS | minify }}
  {{ .Content }}
{{- end }}

Assets directory structure:

assets/
└── scss
    └── main
        └── home.scss

Notes:

  1. When pasting code to forum posts, please use code blocks/fences.
  2. You do not need the leading / when referencing resources in the assets directory.

Thanks for the feedback including the notes.

Your script gives me the well know ZgotmplZ as feedback.

I did not realise the versions have moved up so rapidly. Will update soon.

With error checking and safeCSS:

{{- $id := "foo" -}}
{{- $scss := printf "scss/main/%s.scss" $id -}}
{{- if fileExists (printf "assets/%s" $scss) -}}
  {{- with resources.Get $scss | toCSS | minify }}
    <style>{{ .Content | safeCSS }}</style>
  {{- end }}
{{- end }}

due to the tags that give me ```
{{with}} branches end in different contexts: {stateText delimNone urlPartNone jsCtxRegexp attrNone elementNone }, {stateCSS delimNone urlPartNone jsCtxRegexp attrNone elementStyle }

with foo I get not on file, with home I get now the correct data!
So probably my agony was due to the slash I had or the safeCSS

{{- $id := “foo” -}}
{{- $scss := printf “scss/main/%s.scss” $id -}}
{{- if fileExists (printf “assets/%s” $scss) -}}
{{- with resources.Get $scss | toCSS | minify }}
{{ .Content | safeCSS }}
{{- end }}
{{ else }}
not on file
{{- end }}

Like I said, unable to reproduce. I do not post untested code. Try with current version, or post a link to a public repository for your project. :slightly_smiling_face:

Thanks a ton for your support! I can again continue. Will upgrade immediately.

What you want to do to avoid errors is something like this:

{{ $scss := resources.Get “scss/main/home.scss” }}
{{ if $scss }}
{{ $minified := $scss | toCSS | minify }}
{{ end }}

2 Likes