String concatenation from resource params

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?

Your error comes from using the “wrong dot”.

That can be fixed by doing `{{ $.Scratch.Add … ´

That said, I suspect you will end up with concurrency issues with your construct.

Using scratch, you can work around that with:

{{ $scratch := newScratch }}
{{ $scratch.Add }}

Also note that can rededine variables on Go templates, so you could maybe do this without scratch.

Thank you @bep!

You were correct, using $.Scratch throughout does indeed work.

Using a created new scratch environment I amended my code thusly:

<!-- Identify pages to loop through -->
{{ $homepage  := .Site.GetPage "/interactives" }}
{{ range $index, $value :=  $homepage.Pages.ByWeight }}
<!-- For each page -->
    {{ with $value}}   
        <!-- set overall values -->
        {{ $prefix:= .Params.prefix}}
        {{ $statesetter:= printf "tap:AMP.setState({ %sBar:{" $prefix  }}
        {{ $statecloser:= "}})"}}
        <!-- process each resource-->
        {{ range .Resources.ByType "image" }}
        <!-- prep accumulator variable -->
            {{ $scratch := newScratch }}
            {{ $scratch.Set "statemiddle" "" }}
            <!-- process each option-->
            {{ range $i,$val:=.Params.options}}
                {{  $scratch.Add  "statemiddle" (printf "%s%v: '%s'," $prefix (add $i 1) .name)}}
             {{  $scratch.Add  "statemiddle" (printf "%s%vInfo: '%s'," $prefix  (add $i 1) .info)}}
                {{  $scratch.Add  "statemiddle" (printf "%s%vUrl: '%s'," $prefix  (add $i 1) .url)}}
            {{ end }}
            <!-- construct html -->
            <span on="{{ printf "%s%s%s" $statesetter ($scratch.Get "statemiddle") $statecloser}}" >    
        {{ end }}
    {{ end }}
{{ end }}
1 Like

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