anmith
February 8, 2023, 7:43pm
1
In my page content, I have a image-shortcode which generates and sets an id for every image the shortcode processes.
In the footer of the page, I’d like to loop though all the ids set by the “image”-shortcode on the page.
I played around with Scratch but the output seems to be inconsistent between reloads when using “hugo server”.
How can this be done?
Try .Page.Store
instead of .Page.Scratch
, and let us know if that resolves the issue.
anmith
February 8, 2023, 8:06pm
3
Thanks,
never heard of .Page.Store
but it does the trick.
When looking around for some additional info, I came across this reply in the thread " What is .Page.Store
? " which exactly describes my scenario.
anmith
February 10, 2023, 6:39pm
4
@jmooring : playing around with .Store - is there a way to assign a store to a variable like $myvar := newScratch
?
$myvar := newStore
doesn’t work.
I’ve never had the need for this, but from the docs:
In this case, no Page or Shortcode context is required and the scope of the scratch is only local .
So a newStore
function seems unnecessary.
anmith
February 10, 2023, 9:16pm
6
Collecting all the ids set by the shortcode with .PageStore works perfectly in the “single.html”
The part where I’m struggling is doing the same in the “list.html” where I loop over a range of pages. So my basic idea was to collect the ids within the range loop for each page and then copy the collected ids outside to the list context.
Try this:
git clone --single-branch -b hugo-forum-topic-42856 https://github.com/jmooring/hugo-testing hugo-forum-topic-42856
cd hugo-forum-topic-42856
hugo server
shortcode
{{ $id := printf "foo-%d" .Ordinal }}
{{ .Page.Store.Add "instances_of_foo" (slice $id) }}
single.html
{{ if .HasShortcode "foo" }}
<pre>{{ jsonify (dict "indent" " ") (.Page.Store.Get "instances_of_foo") }}</pre>
{{ end }}
list.html
{{ $s := slice }}
{{ range site.RegularPages }}
<h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ if .HasShortcode "foo" }}
{{ $s = $s | append (dict "path" .File.Path "ids" (.Store.Get "instances_of_foo") ) }}
{{ end }}
{{ end }}
<pre>{{ jsonify (dict "indent" " ") (sort $s "path") }}</pre>
1 Like
anmith
February 11, 2023, 3:04pm
8
@jmooring : Thanks a lot - played around with your test site and it does everything I was trying to solve.
Using a slice instead of trying to work with an outer and an inner store seems a way better idea than doing what I was trying to do.
system
Closed
February 13, 2023, 3:04pm
9
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.