Conditionally load CSS

I want to conditionally include a CSS file and am wondering if/how that’s feasible. What I’m trying to do:

  • in a partial that is executed in the main part of the page, use $.Scratch.Set "include" "true"
  • in the footer template (which is executed well after the first one) inquire the value of $.Scratch.Get "include".

However, $.Scratch.Get "include" always returns nil. Is there a reliable way to achieve what I want, or should I simply drop the idea?

I’m aware that I could use .hasShortcode, but in my context, there is no shortcode in use.

$.Store may helps, similar to Scratch but won’t be reset on server rebuilds.

Thanks, but that doesn’t work either. I’m probably doing something stupid somewhere…

From your examples above, it looks like both partials expect to receive a page in context.

Do your partial calls pass the page as context? Or to put it another way, are you missing the “dot” anwhere?

{{ partial "footer.html" . }}
{{ partial "thing-that-does-the-include.html" . }}
2 Likes

That was it. I should probably add “of course”. Too stupid.

Just one related question: I’d rather add the CSS to the header template conditionally. But as that is executed before the one that might set the Scratch, I suppose that’s not possible?

in a partial that is executed in the main part of the page

Is the partial called from a shortcode, either directly or indirectly?

No, there’s no shortcode involved, neither directly nor indirectly

Correct. If you were calling the partial from a short code, we could evaluate Content at the top of the template, which should bring the Scratch into scope.

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