Long form of the question: Is there any way to associate CSS (and why not JS) with a specific partial and ensure it’s included in the <head>
(or before the body end tag for scripts) html section only when that partial is used?
The story behind that question
Initially I tried to accomplish this by using blocks. The idea was to create a block in the <head>
section of my baseof.html file like {{ block "my-partial-specific-style" . }}
and then define that block in my partial like so:
{{ define "my-partial-specific-style" }}
<style>
...css here
</style>
{{ end }}
...partial code
It doesn’t work for partials. It may be used for other page templates but in my case all “partial specific” blocks are de facto defined regardless of whether the corresponding partial is called or not from inside the given template/layout file. I read somewhere in the forum that blocks are not intended to be used inside partials.
Then I thought about using conditional logic with scratch variables. This time the idea was to set a scratch variable (think of it as a flag) from within a partial template like so:
{{ .Scratch.Set "useMyPartial" true }}
...partial code
Remember that this inevitably involves the passing of a “Scratch” param or the whole page context, meaning that I had to call my partial like {{ partial "myPartial" (dict "Scratch" .Scratch "otherParam" $otherParam }}
or {{ partial "myPartial" (dict "context" . "otherParam" $otherParam }}
to be able to set scratch variables from within the partial.
However, when I tried to get that .Scratch variable from within the <head>
section of my baseof.html and then apply some conditional logic to render the desired “partial specific” inline css:
{{ if (.Scratch.Get "useMyPartial") }}
<style>
...css here
</style>
{{ end }}
Guess what happened? It didn’t work either! The problem this time was with the timing of setting and getting the scratch variable. I just tried to get something which is not set because obviously I want my partial (with the set function) to be rendered after the section (with the get function). So, the scratch variables didn’t do the trick…
And now I am stuck. The only “solution” I found is to use front matter parameters to list all the css needed for example .css file names and then render those files with <link>
attributes or however you’d like in the baseof.html.
But that is not what would have been very nice to be and namely a system where partials have associated stylesheets/scripts which render in the <head>
(for the stylesheets) and just before the body end tag (for the scripts) dynamically only when the partial is called.
Is there any other (even a bit) more convenient (than the front matter one) way of doing something similar? I just want to use partials in a modular and structured way by loading the css and javascript they need to work and look pretty only when they are used in the given page template…
Thanks in advance for the patience to read the whole story and my question. Hope someone share a good practice or approach for tackling the challenge!