Hi all
I have a post metadata bar in a partial partials/post_meta.html
that appears under the post title in _default/single.html
. I also want to use the same partial in post lists on index.html
and section/post.html
. However, I need to simplify the contents of the metadata partial in cases where it appears on one of these two list pages. Therefore, I considered Scratch as a potential solution.
The code works fine (simplifying the metadata partial only on the homepage and not on the single page) until I add Scratch to the section/post.html
, when the partial now thinks on every page that it’s in a list.
Code is below, any ideas - I guess it’s a scoping issue, or perhaps there’s a better way to achieve this without Scratch?
Thanks!
index.html
{{ range first 5 (where .Data.Pages "Type" "post") }}
{{ .Scratch.Add "is_list" 1 }}
{{ partial "post_meta" . }}
{{ end }}
partials/post_meta.html
{{ $is_list := .Scratch.Get "is_list" }}
{{ if $is_list }}
<a href="{{ .Permalink }}">Read</a>
brief post metadata
{{ else }}
full post metadata
{{ end }}
_default/single.html
{{ partial "post_meta" . }}
section/post.html
{{ range .Data.Pages.GroupByDate "2006" }}
<h3>{{ .Key }}</h3>
{{ range .Pages.ByDate.Reverse }}
{{ .Scratch.Add "is_list" 1 }}{{ partial "post_meta" . }}
{{ end }}
{{ end }}