Assigning Scratch variable values to non Scratch variables

Hi there,

I’m new to Hugo, and I find that a major pain point of working with templates is the inability to assign values to existing variables. For example,

{{ $foo := "Hello" }}
{{ $foo = "world" }} // THIS DOESN'T WORK!
{{ $foo }}

does not work. However, I noticed the very handy Scratch function, which allows operations like this:

{{ $.Scratch.Add "foo" "Hello"}}
{{ $.Scratch.Set "foo" "world"}}
{{ $.Scratch.Get "foo" }} //Outputs "world"

However, I have a use case where I’d want to use the value of $foo in the Scratch scope. I’ve tried {{ $bar := $.Scratch.Get "foo"}}, but this doesn’t seem to be possible. Is there a way to extract the value of a Scratch variable? Or even better, a way to re-assign values to variables in the templates? Thank you.

1 Like

That syntax should work fine (I use it in several places). Can you post an example where it isn’t working for you?

-j

@jgreely thanks so much for the response. Turns out for some reason my build wasn’t autoupdating (I think my drafts flag was off when I started the server, and I’d changed my post to be a draft). That syntax does indeed work. Sorry for the trouble, but thanks again, much appreciated.