refactor(layouts): replace scratch with template variables

- replaced deprecated .Scratch with local variables in toc, index.json, share_icons and ltr/rtl shortcodes
- replaced newScratch with local variables in post_meta, schema_json and share_icons
- keeps state local to each template; output unchanged
This commit is contained in:
Aditya Telange committed 2026-10-18 20:02:03 +05:30
1 parent a5d59c7d44
commit a87aee984e
7 files changed
+31 -36

No files matched your search

+6 -6
View File
@@ -1,25 +1,25 @@
{{- $scratch := newScratch }}
{{- $meta := slice }}
{{- if not .Date.IsZero -}}
{{- $scratch.Add "meta" (slice (printf "<span title='%s'>%s</span>" (.Date) (.Date | time.Format (default ":date_long" site.Params.DateFormat)))) }}
{{- $meta = $meta | append (printf "<span title='%s'>%s</span>" (.Date) (.Date | time.Format (default ":date_long" site.Params.DateFormat))) }}
{{- end }}
{{- if (.Param "ShowReadingTime") -}}
{{- $scratch.Add "meta" (slice (printf "<span>%s</span>" (i18n "read_time" .ReadingTime | default (printf "%d min" .ReadingTime)))) }}
{{- $meta = $meta | append (printf "<span>%s</span>" (i18n "read_time" .ReadingTime | default (printf "%d min" .ReadingTime))) }}
{{- end }}
{{- if (.Param "ShowWordCount") -}}
{{- $scratch.Add "meta" (slice (printf "<span>%s</span>" (i18n "words" .WordCount | default (printf "%d words" .WordCount)))) }}
{{- $meta = $meta | append (printf "<span>%s</span>" (i18n "words" .WordCount | default (printf "%d words" .WordCount))) }}
{{- end }}
{{- if not (.Param "hideAuthor") -}}
{{- with (partial "author.html" .) }}
{{- $scratch.Add "meta" (slice (printf "<span>%s</span>" .)) }}
{{- $meta = $meta | append (printf "<span>%s</span>" .) }}
{{- end }}
{{- end }}
{{/* Combine all meta information into a single string with separators and render it as HTML.*/}}
{{- with ($scratch.Get "meta") }}
{{- with $meta }}
{{- delimit . "&nbsp;·&nbsp;" | safeHTML -}}
{{- end -}}
+5 -5
View File
@@ -1,12 +1,12 @@
{{- $pageurl := .Permalink }}
{{- $title := .Title }}
{{- $.Scratch.Set "tags" ""}}
{{- $tags := ""}}
{{- with .Params.Tags }}
{{- $hashtags := newScratch}}
{{- range . }}{{ $hashtags.Add "tags" (slice (replaceRE "(\\s)" "" . ))}}{{end}}
{{- $.Scratch.Set "tags" (delimit ($hashtags.Get "tags") ",") }}
{{- $hashtags := slice }}
{{- range . }}{{ $hashtags = $hashtags | append (replaceRE "(\\s)" "" . ) }}{{end}}
{{- $tags = delimit $hashtags "," }}
{{- end -}}
{{- $custom := false }}
@@ -17,7 +17,7 @@
{{- if (or (cond ($custom) (in $ShareButtons "x") (true)) (cond ($custom) (in $ShareButtons "twitter") (true))) }}
<li>
<a target="_blank" rel="noopener noreferrer" aria-label="share {{ $title | plainify }} on x"
href="https://x.com/intent/tweet/?text={{ $title }}&amp;url={{ $pageurl }}&amp;hashtags={{- $.Scratch.Get "tags" -}}">
href="https://x.com/intent/tweet/?text={{ $title }}&amp;url={{ $pageurl }}&amp;hashtags={{- $tags -}}">
<svg version="1.1" viewBox="0 0 512 512" xml:space="preserve" height="30px" width="30px" fill="currentColor">
<path
d="M512 62.554 L 512 449.446 C 512 483.97 483.97 512 449.446 512 L 62.554 512 C 28.03 512 0 483.97 0 449.446 L 0 62.554 C 0 28.03 28.029 0 62.554 0 L 449.446 0 C 483.971 0 512 28.03 512 62.554 Z M 269.951 190.75 L 182.567 75.216 L 56 75.216 L 207.216 272.95 L 63.9 436.783 L 125.266 436.783 L 235.9 310.383 L 332.567 436.783 L 456 436.783 L 298.367 228.367 L 432.367 75.216 L 371.033 75.216 Z M 127.633 110 L 164.101 110 L 383.481 400.065 L 349.5 400.065 Z" />
+3 -3
View File
@@ -26,7 +26,7 @@
{{- $lang_url := strings.TrimPrefix ( printf "%s/" .Lang) $url }}
{{- $bc_list := (split $lang_url "/")}}
{{- $scratch := newScratch }}
{{- $path := "" }}
<script type="application/ld+json">
{
"@context": "https://schema.org",
@@ -34,8 +34,8 @@
"itemListElement": [
{{- range $index, $element := $bc_list }}
{{- $scratch.Add "path" (printf "%s/" $element ) | safeJS }}
{{- $bc_pg := site.GetPage ($scratch.Get "path") -}}
{{- $path = printf "%s%s/" $path $element }}
{{- $bc_pg := site.GetPage $path -}}
{{- if (and ($bc_pg) (gt (len . ) 0))}}
{{- if (and $index)}}, {{end }}
+6 -11
View File
@@ -21,11 +21,11 @@
{{- $firstHeaderLevel := len (seq (index (findRE "[1-6]" (index $headers 0) 1) 0)) -}}
{{- $.Scratch.Set "bareul" slice -}}
{{- $bareul := slice -}}
<ul>
{{- range seq (sub $firstHeaderLevel $largest) -}}
<ul>
{{- $.Scratch.Add "bareul" (sub (add $largest .) 1) -}}
{{- $bareul = $bareul | append (sub (add $largest .) 1) -}}
{{- end -}}
{{- range $i, $header := $headers -}}
{{- $headerLevel := index (findRE "[1-6]" . 1) 0 -}}
@@ -46,22 +46,17 @@
<ul>
{{/* the first should not be recorded */}}
{{- if ne $prevHeaderLevel . -}}
{{- $.Scratch.Add "bareul" . -}}
{{- $bareul = $bareul | append . -}}
{{- end -}}
{{- end -}}
{{- else -}}
</li>
{{- if lt $headerLevel $prevHeaderLevel -}}
{{- range seq (sub $prevHeaderLevel 1) -1 $headerLevel -}}
{{- if in ($.Scratch.Get "bareul") . -}}
{{- if in $bareul . -}}
</ul>
{{/* manually do pop item */}}
{{- $tmp := $.Scratch.Get "bareul" -}}
{{- $.Scratch.Delete "bareul" -}}
{{- $.Scratch.Set "bareul" slice}}
{{- range seq (sub (len $tmp) 1) -}}
{{- $.Scratch.Add "bareul" (index $tmp (sub . 1)) -}}
{{- end -}}
{{- $bareul = first (sub (len $bareul) 1) $bareul -}}
{{- else -}}
</ul>
</li>
@@ -81,7 +76,7 @@
{{- $lastHeaderLevel := len (seq (index (findRE "[1-6]" (index $headers (sub (len $headers) 1)) 1) 0)) }}
</li>
{{- range seq (sub $lastHeaderLevel $firstHeaderLevel) -}}
{{- if in ($.Scratch.Get "bareul") (add . $firstHeaderLevel) }}
{{- if in $bareul (add . $firstHeaderLevel) }}
</ul>
{{- else }}
</ul>
+4 -4
View File
@@ -1,13 +1,13 @@
{{ $.Scratch.Set "md" false }}
{{ $md := false }}
{{ if .IsNamedParams }}
{{ $.Scratch.Set "md" (.Get "md") }}
{{ $md = .Get "md" }}
{{ else }}
{{ $.Scratch.Set "md" (.Get 0) }}
{{ $md = .Get 0 }}
{{ end }}
<div dir="ltr">
{{ if eq ($.Scratch.Get "md") false }}
{{ if eq $md false }}
{{ .Inner }}
{{ else }}
{{ .Inner | markdownify }}
+4 -4
View File
@@ -1,13 +1,13 @@
{{ $.Scratch.Set "md" false }}
{{ $md := false }}
{{ if .IsNamedParams }}
{{ $.Scratch.Set "md" (.Get "md") }}
{{ $md = .Get "md" }}
{{ else }}
{{ $.Scratch.Set "md" (.Get 0) }}
{{ $md = .Get 0 }}
{{ end }}
<div dir="rtl">
{{ if eq ($.Scratch.Get "md") false }}
{{ if eq $md false }}
{{ .Inner }}
{{ else }}
{{ .Inner | markdownify }}
+3 -3
View File
@@ -1,7 +1,7 @@
{{- $.Scratch.Add "index" slice -}}
{{- $index := slice -}}
{{- range site.RegularPages -}}
{{- if and (not .Params.searchHidden) (ne .Layout `archives`) (ne .Layout `search`) }}
{{- $.Scratch.Add "index" (dict "title" .Title "content" .Plain "permalink" .Permalink "summary" .Summary) -}}
{{- $index = $index | append (dict "title" .Title "content" .Plain "permalink" .Permalink "summary" .Summary) -}}
{{- end }}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}
{{- $index | jsonify -}}