.Scratch within partial range

I’m trying to use .Scratch in a range within a partial file. The goal is to set a .Scratch value outside of the range, change the scratch value in the range, then display the new value outside of the range. However, I can’t seem to even display the existing scratch value in the range.

In my main file, I’m using dict to pass context:

{{ partial "sections/build.html" (dict "c" . "g" $ ) }}`

In my partial file I’m able to set a scratch and display it properly. However, this doesn’t work within the range.

<!--. is .c-->
<!--$ is .g-->

{{ .g.Scratch.Set "number" "seven" }}

<!--displays seven correctly-->
{{ .g.Scratch.Get "number" }}
          
    {{ range .c.row1 }}
    <!--doesn't return anything-->
        {{ .g.Scratch.Get "number" }}
    {{ end }}

I know that I could set a variable equal to the scratch value and then display my scratch within the range this way. However, then I can’t reference the new scratch value outside of the range.

I would store your .g.Scratch in a variable instead. I did not test this, but I’m pretty sure, it would be accessible later on.

{{ $scratch := .g.Scratch }}
{{ $scratch.Set "number" "seven" }}
{{ range .c.row1 }}
    {{ $scratch.Get "number" }}
 {{ end }}

You solved my problem, thank you!

I was previously creating the variable like this:

{{ .g.Scratch.Set "number" "seven" }}
{{ $number := .g.Scratch.Get "number" }}

Glad I could help! :slight_smile:
FIY: You can now mark your issue as “solved” by clicking the three little dots below the comment which solved it and clicking the checkbox. This way the thread is marked as solved!

1 Like