"too many declaration in range"

why is the following accepted

{{- range $dest, $title := (.Store.Get "all_sources") -}}
{{ $title }}, {{ $dest }}
{{ end }}

While this:

{{- range $permalink, $srcset, $sizes := (.Store.Get "preload_list") -}}
{{end}}

is considered "too many declarations in range ?? There are three variables, because preloast-list has three components. The exact same structure as the first code ({{ .Page.Store.SetInMap "all_sources" .Destination (default .Text .Title) }}) except with one more component.

I think you misunderstand the syntax.

{{ range $k, $v := $vals }}

The $k represents the index/key of slice/map, the $v represent the value of each element.

See Templating | Hugo.

is what I read, but then I don’t understand my code anymore. So

{{ .Page.Store.SetInMap “all_sources” .Destination (default .Text .Title) }}

.Destination is a key and the rest is its value ?
In that case it sucks, because the solution that worked with all_sources can’t work here.
What I need, is to store sets of three values (if possible in a dict) in the same page-wide variable.
I tried the equivalent of

{{ .Scratch.Set "greeting" "Hello" }}
{{ .Scratch.Add "greeting" "Welcome" }}
{{ .Scratch.Get "greeting" }}

But .Add is interpreted as the numerical method. Probably.

template: home/_markup/render-image.html:61:9: executing “partials/ImagePartial” at <.Page.Store.Add>: error calling Add: can’t apply the operator to the values

{{ .Page.Store.Add “preload-list” (dict “srcset” (delimit $src , ) “permalink” $medium.RelPermalink “sizes” “(max-width: 400px) 100vw, (max-width: 604px) 50vw, 42.5vw”) }}

The range accepts declarations no more than 2, and none of the decalrations represent the element’s member. So you should tweak the code as follows (similar) in your case.

{{ .Store.SetInMap "preload_list" "foo" (dict "permalink" "/foo" "srcset" "foo-srcset" "sizes" 100) }}
{{ .Store.SetInMap "preload_list" "bar" (dict "permalink" "/bar" "srcset" "bar-srcset" "sizes" 200) }}
{{ range $i, $preload := (.Store.Get "preload_list") -}}
  {{ $preload.permalink }}
  {{ $preload.srcset }}
  {{ $preload.sizes }}
{{ end }}

Keys:

  1. The preload_list should be an slice/map.
  2. .Store.SetInMap "preload_list" "foo" specified a key via the second arg (foo/bar in this case) for keeping elements to be unique.

But I don’t need “foo” or “bar”, can’t I just stack the dict value one on top of the others ?
what’s wrong with {{ .Page.Store.Add "preload-list" (dict "srcset" (delimit $src , ) "permalink" $medium.RelPermalink "sizes" $sizes) }} ?

But I don’t need “foo” or “bar”

You’ll be need it, since you will get multiple dulplicate items when content changes (Store.Add).

Just slice the dict and add it to store.

{{ .Page.Store.Add "preload-list" (slice (dict "srcset" (delimit $src ` , `) "permalink" $medium.RelPermalink "sizes" $sizes)) }}

That’s all I can tell, all of those use cases are already documented, you can take a close look into the documentations or read again.

Oh, slice with the add method ! that’s what I forgot ! thanks !
Yes, all is documented… but not completely, not all use cases, and even if they are, the syntax is so awkward that it’s not always easy to read the sentences without feeling the conjunctivitis arriving.
Now I have one last problem…
with this line:

{{ with eq .Attributes.loading “eager” }}
{{ .Page.Scratch.Add “preload-list” (slice (dict “srcset” (delimit $src ,) “permalink” $medium.RelPermalink “sizes” $sizes)) }}
{{end}}

and merely one images having the loading=eager attribute, and ALL the images of the page get their preload link tag. All 24 of them. Store or Scratch don’t make any difference.

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