Use conditional for css style with shortcode

I’m not sure. The thing is, in most situations, packaging all of the CSS to cover all scenarios (shortcodes etc.) and include them in every page is the simplest and good enough.

The problem with your approach is that your template needs to know about the shortcode.

What you could consider doing instead is having the shortcode telling the template what it needs, e.g. in the shortcode template:

{{ $stylesNeeded := slice "css/conditional/shortcodes/blockquote.css" "css/conditional/shortcodes/foo.css" }}
{{ $.Page.Scratch.Add "styles" $stylesNeeded }}

Then in your single.html template (or something):

{{/* Need to invoke .Content to render the shortcodes. */}}
{{ $content := .Content }}
{{ with .Scratch.Get "styles" }}
{{ range (. | uniq) }}
// Get the resource and create a link
{{ end }}
{{ end }}
4 Likes