Calling site parameters in a partial to build Tailwind CSS

I’m attempting to use the example css.html partial from the css.Tailwindcss documentation (as below) (this is my modified version):


{{ with (templates.Defer (dict "key" "global")) }}
  {{ $theme := $.Site.Params.colorScheme | default "slate" }}
  {{ with resources.Get (printf "css/%s.css" $theme) }}
  {{/* with resources.Get "css/test.css" */}}
    {{ $opts := dict
      "minify" (not hugo.IsDevelopment)
      "inlineImports" true
    }}
    {{ with . | css.TailwindCSS $opts }}
      {{ if hugo.IsDevelopment }}
        <link rel="stylesheet" href="{{ .RelPermalink }}">
      {{ else }}
        {{ with . | fingerprint }}
          <link rel="stylesheet" href="{{ .RelPermalink }}" integrity="{{ .Data.Integrity }}" crossorigin="anonymous">
        {{ end }}
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

Now, I think I understand that because of templates.Defer it doesn’t have access to the site parameters, etc? What do I need to do in order to reference them in this partial?

(the reason for doing this is to allow the theme user to set the color scheme)

looks like you have two options:

  • add a “data” key-value pair to the options passed to templates.Defer: see templates.Defer - Options and the example there.
    {{ $options := dict (
       "key" "global"
       "data" (dict  "mykey1" val2 "myval2" val2 ...)
    ) }}
    {{ templatesDefer $options }}
    
  • use the global site function instead of $.Site
1 Like

That did it! Thanks!

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