I am curious if there is a site-wide Scratch function? I have everything working as I would like, but it seems the $.Scratch is page-wide and not scoped to the entire site.
FWIW I am incrementing a value every time a post is encountered throughout the site and then using that (modded) value to access data from the .Site.Data collection.
It would be great to know if I am overlooking something obvious here to accomplish what I am trying to achieve as well.
Correct, the .Pages returns the total collection of pages, thank you for your assistance @zwbetz.
What I am interested in is when a page is generated (or rendered) and at that time increment a site-wide variable, essentially storing a variable (or state) across all known pages that I can not only read, but write/set.
Scratch works perfectly but it seems to be page-centric and not site-wide.
Hmm yeah am not sure how to accomplish this with Scratch (if it’s even possible).
I’d recommend using my example above and tweaking the where to filter down to only the pages you want.
It also depends on what you’re storing in this global variable. I was assuming it was a count of all pages you cared about, but if it’s something else then yeah my example won’t fit your needs.
OK that is good feedback. I will try to see if I can better explain my scenario.
Imagine I have a partial, with the following expression peppered throughout my templates:
{{ partial "stateful-template" . }}
Where stateful-template looks like the following:
{{ .Scratch.Add "SiteIndex" 1 }}
{{ $index := mod (.Scratch.Get "SiteIndex") (len .Site.Data.MyCollection) }}
{{ .Scratch.Set "SiteIndex" $index }}
{{ with index .Site.Data.MyCollection $index }}
...
{{ end }}
This works fine when I iterate on a particular page, but when another page is rendered, the index is reset, even if I use $.Scratch. This makes it seem that $.Scratch is page-wide and not site-wide.
Ideally there would be a $.Site.Scratch but this does not seem to exist. Which makes me suspect there is a much more obvious/preferred way to accomplish what I am aiming to achieve here.
Are you able to share your code? Or if you can’t, then create a small project that replicates your issue. It’d be much easier to help you this way. So that I and others can see the full picture.