Best method to collect all sources/external URLs

Hi,

{{ .Page.Scratch.Add “all_sources” (slice $.Text $destination) }}

is returning returning me what seems to be a slice with only one string, containing all links, with .Text and $destination.
Separated by space, or not, depends. It’s a mess.
I expected a map or slice of strings, which I could use delimit on.
I read the docu and don’t understand.
The result of {{ delimit (.Page.Scratch.Get "all_sources") " "}} is

error calling delimit: can’t iterate over <nil>  failed to render pages: can’t iterate over <nil> 

One or more of the pages rendered with this code does not have matching Scratch. You need to code defensively.

Ah… Ok
Still, more documentation and examples of use cases wouldn’t hurt.
One more question : why does the result of this:

{{with (.Page.Scratch.Get "all_sources")}}
	{{ delimit . "<p>"}}
{{ end }}

(with the command hugo server) changes when it reloads, when I save the text editor on such or such file ? It appears as it should with when saving render-link.html but is empty/outputs nothing when saving on content-after.html.
But that difference doesn’t happen with .Store instead of .Scratch.
Is this a feature or a bug ?
And I don’t understand what .Get is returning. I would expect a slice of slices,
but it’s not the case because {{range . }}<p>{{ delimit . " "}}{{end}} is returning me gibberish numbers like 104 116 116 112 115 58 47 47 119 119 119 46 103 105 103 97 108 114 101 115 101 97 114 99 104 46 99 111 109 47 117 107 47 112 117 98 108

None of the entries were numbers or close, it shouldn’t happen…
Damn weakly-typed languages.

.Get returns what you .Set. This is a “you” problem, not a Hugo problem.

so {{ .Page.Store.Add "all_sources" (slice $.Text $destination) }} followed by {{{ .Page.Get "all_sources"}} should return a slice of slices, right ? .Text being a string and $destination too.

.Add doesn’t return anything. It adds something to an existing something that you initialized with .Set.

{{ .Page.Store.Set "foo" (slice "aaa" "bbb") }}

{{ .Page.Store.Get "foo" }} --> [aaa bbb] 

{{ .Page.Store.Add "foo" (slice "ccc" "ddd") }}

{{ .Page.Store.Get "foo" }} --> [aaa bbb ccc ddd]

https://gohugo.io/functions/scratch/#add

If the first Add for a key is an array or slice, the following adds will be appended to that list.

I meant .Get, sorry.
Yes I read that sentence but didn’t understand. Now I do.
How can I get the object to be a slice of slices instead ?
Like [[aaa bbbb] [cccc ddd]]
also I noticed that I didn’t initiliaze the object, yet it doesn’t seem to matter. Or perhaps it caused the erratic behavior I described?

Yes, it does.

If you want it to be a slice of slices, you need to give it a slice of slices.

{{ .Page.Store.Set "foo" (slice (slice "aaa" "bbb") (slice "ccc" "ddd")) }}
{{ .Page.Store.Get "foo" }} --> [[aaa bbb] [ccc ddd]]
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.