Don't use scratchpad to pass values into partials

We previously used .Scratch which is a global scratchpad, that was later
passed over to the partial with the rest of the context. The partial
then checked if this value was set, and had a fallback if it wasn't
(default value).

This approach does work in most of the cases, however in about 5% of
runs, it fails. I assume this is due to hugo spinning up multiple
threads, and each of them is changing this global scratchpad in
arbitrary order. At some point, a thread then removed this value from
the scratchpad to reset it, but another thread already checked that this
variable does exist, and has now set the no longer available variable.

To avoid this, we can instead directly pass a custom dict, which
contains the original context (.) along with another variable, which
will hold the dateformat. This however means that we can't use the
simple syntax with a default value, and the dateformat will need to be
set each time (though technically, it could be set to nil and checked
later, but if we're already specifying it, we may as well pass in this
dateformat directly)
This commit is contained in:
ItsDrike 2022-05-14 22:26:04 +02:00
parent 955d6cb0cb
commit b444f03185
No known key found for this signature in database
GPG key ID: B014E761034AF742
3 changed files with 13 additions and 16 deletions

View file

@ -11,7 +11,7 @@
<ul class="item-list-items">
{{ range .Pages.ByTitle }}
<li class="item-list-item" data-id="{{ with .File}}{{ .File.UniqueID }}{{ end }}">
{{ partial "list_item.html" . }}
{{ partial "list_item.html" (dict "context" . "dateformat" "Jan 02, 2006") }}
</li>
{{ end }}
</ul>

View file

@ -1,18 +1,16 @@
<!-- Obtain "dateformat" from scratchpad (or fall back to default) -->
{{ if not (.Scratch.Get "dateformat") }}
{{ .Scratch.Set "dateformat" "Jan 02, 2006" }}
{{ end }}
{{ $dateformat := .Scratch.Get "dateformat" }}
<!-- Reset scratchpad for next use (in next page) - avoids using old values -->
{{ .Scratch.Set "dateformat" nil }}
<!--
Note: This partial is expected to be called with special syntax of:
partial "list_item.html" (dict "context" . "dateformat" "Jan 02, 2006")
This allows it to take "dateformat" as a variable, so it can differ between calls.
-->
<a class="item-name" href="{{ .RelPermalink }}">
<span class="item-title">{{ .Title }}</span>
{{ if not .Date.IsZero }}
<span class="item-day">{{ .Date.Format $dateformat }}</span>
<a class="item-name" href="{{ .context.RelPermalink }}">
<span class="item-title">{{ .context.Title }}</span>
{{ if not .context.Date.IsZero }}
<span class="item-day">{{ .context.Date.Format .dateformat }}</span>
{{ end }}
</a>
<small class="item-details">{{ partial "content-details.html" . }}</small>
<small class="item-details">{{ partial "content-details.html" .context }}</small>
<!-- TODO: Only show summary on hover? -->
<p class="item-summary">{{ partial "summary.html" . }}</p>
<p class="item-summary">{{ partial "summary.html" .context }}</p>

View file

@ -26,8 +26,7 @@
<ul class="item-list-items">
{{ range .Pages }}
<li class="item-list-item" data-id="{{ with .File}}{{ .File.UniqueID }}{{ end }}">
{{ .Scratch.Set "dateformat" "Jan 02" }}
{{ partial "list_item.html" . }}
{{ partial "list_item.html" (dict "context" . "dateformat" "Jan 02") }}
</li>
{{ end }}
</ul>