A render hook writes a value to the page store:
{{ .Page.Store.Set "hasFeature" true }}
The page head reads that value to conditionally include CSS.
I have found two working approaches.
Approach 1: force content rendering before reading the store:
{{ $noop := .Content }}
{{ if .Store.Get "hasFeature" }}
<link rel="stylesheet" href="...">
{{ end }}
This is similar to the pattern currently shown in the Hugo documentation,
where .WordCount is evaluated before Store.Get.
Approach 2: defer the consumer:
{{ $data := dict "page" . }}
{{ with templates.Defer (dict "data" $data) }}
{{ if page.Store.Get "hasFeature" }}
<link rel="stylesheet" href="...">
{{ end }}
{{ end }}
Both approaches appear to work.
The first keeps the dependency local and seems easier to reason about.
Rendered content is cached, so a later .Content access should not perform
the full conversion again.
The second avoids explicitly rendering content from the head, but introduces
a deferred placeholder and final output rewriting. The documentation describes
templates.Defer as intended for rare use cases.
For a single page and output format, where render hooks populate Page.Store
and the head consumes it:
- Is either approach considered preferable?
- Are there correctness, concurrency, caching, server rebuild, or output-format
differences that should influence the choice? - Is
templates.Deferprimarily intended for values that cannot be complete
until later in the site build, rather than this page-local dependency?