I’m want some specific CSS included in a page if a shortcode is used, and I want that CSS to be dynamic based on some Params set in the front matter.
In layouts/partials/head.html
I have the following:
{{- if (.Page.HasShortcode "myShortcode") -}}
{{- $finalCSSPath := print .Page.RelPermalink "css/myShortcode.css" }}
{{- $shortcodeCSS := resources.Get "templates/myShortcode.css" | resources.ExecuteAsTemplate $finalCSSPath . }}
<link rel="stylesheet" type="text/css" href="{{ $shortcodeCSS.RelPermalink }}">
{{- end -}}
which generates the param based css and outputs it to css/myShortcode.css
relative to the page that included the shortcode.
That was working fine, but I just added a second page with the same shortcode, and now both pages always get the same generated CSS from the template, so it’s not specific for each page. Also, when I look at how many times the "templates/myShortcode.css"
file is executed, it’s only run once.
cumulative average maximum
duration duration duration count template
---------- -------- -------- ----- --------
17.174424ms 8.587212ms 9.323163ms 2 _default/single.html
11.059626ms 3.686542ms 5.52871ms 3 partials/head.html
3.050797ms 3.050797ms 3.050797ms 1 templates/compare.css
So the head.html partial is being run 3 times (correct: one list page, two pages with the shortcode, but the template is only being called once.
Does the resources.ExecuteAsTemplate cache or am I not understanding how it’s supposed to work? Wanted to check if there’s an known/obvious reason for this before I try to recreate in a simple test repo.
Using Hugo Static Site Generator v0.69.0/extended darwin/amd64 BuildDate: unknown