I want to store an array of maps in .Page.Scratch. These maps are added dynamically via a short code - i.e. I want to push a map to an array of maps in .Page.Scratch. This is what I tried initially:
For context - this is a short code for FAQ entries that is applied like so:
{{% faq "The question" %}}
The answer.
{{% /faq %}}
I then later want to iterate over all FAQs in a partial and output them as JSON-LD meta data.
However, {{ .Page.Scratch.Add "faqs" $faq }} yields the following error:
failed to process shortcode: "faq.html:21:12": execute of template failed at <.Page.Scratch.Add>: error calling Add: can’t apply the operator to the values
I am sure there is a way to dynamically store an array of maps in a scratch but I am unable to figure out how
I saw .SetInMap but did not consider it since it would require an additional identifier. So there is no way to just push elements to an array that is in turn stored in .Store or .Scratch?
It looks like you are trying to use .Add with .GetSortedMapValues. That won’t work. You need to .SetInMap instead of add. Here’s a dirt simple example:
git clone --single-branch -b hugo-forum-topic-44677 https://github.com/jmooring/hugo-testing hugo-forum-topic-44677
cd hugo-forum-topic-44677
hugo server