Core problem: unable to use scratch for easy text concatenation inside resources loop
I need to build up complex html and js on a page to build interactive sections of a homepage but I’m having trouble getting the string concatentation work via the Scratch method. Recommendations welcome!
My content pages have yaml like the following. There are many resources with many options.
---
title: Hi
prefix: project
resources:
- name: thing1
title: Thing 1
src: automotive.svg
params:
options:
- name: Part 1
info: somestuff
- name: Part 2
info: somestuff
---
I need to be able to generate templated HTML like the following for each resource.
<span data-tooltip-title="{{.Title}}" on="tap:AMP.setState({ {{prefix}}Bar:{
{{prefix}}{{ .option.$i}}:{{ .option.$i.name}},
{{prefix}}{{ .option.$i}}info:{{ .option.$i.info}},
...
}
})"
></ span>
I have constructed a partial to be able to do this but it errors around the use of Scratch in the <!-- 2. Process each resource-->
loop.
<!-- Identify pages to loop through -->
{{ $homepage := .Site.GetPage "/interactives" }}
{{ range $index, $value := $homepage.Pages.ByWeight }}
<!-- 1. For each page -->
{{ with $value}}
<!-- set overall values -->
{{ $prefix:= .Params.prefix}}
{{ $statesetter:= printf "tap:AMP.setState({ %sBar:{" $prefix }}
{{ $statecloser:= "}})"}}
<!-- 2. Process each resource-->
{{ range .Resources.ByType "image" }}
<!-- prep accumulator variable -->
{{ .Scratch.Set "statemiddle" "" }}
<!-- 3. process each option-->
{{ range $i,$val:=.Params.options}}
<!-- accumulate -->
{{ .Scratch.Add "statemiddle" (printf "%s%s: '%s'," $prefix (add $i 1) .name)}}
{{ .Scratch.Add "statemiddle" (printf "%s%sInfo: '%s'," $prefix (add $i 1) .info)}}
{{ .Scratch.Add "statemiddle" (printf "%s%sUrl: '%s'," $prefix (add $i 1) .url)}}
{{ end }}
<!-- construct html -->
<span on="{{ printf "%s%s%s" $statesetter (.Scratch.Get "statemiddle") $statecloser}}" >
{{ end }}
{{ end }}
{{ end }}
The error generated is:
executing “partials/home/interactivelooper.html” at <.Scratch.Set>: can’t evaluate field Scratch in type resource.Resource
Is there an alternative way to accumulate / concatenate these values into strings within each resource?