Go Template Syntax for assigning result of piping to a variable

I am trying to retrieve a JSON file based on the environment that the static pages should be built for (qa, staging, prod etc) and am having issues trying to figure out the best way of doing this within Go templates.

I have the following but am uncertain how to assign the result of the first to the variable $pathToFile so that the hardcoded path on line 2 can be removed. I’ve tried a few things but they have resulted in the template not being processed.

    {{ getenv "TARGET_ENV" | printf "data/%s/config.json" | getJSON }} // the result of this needs assigning to $pathToFile

    {{ $pathToFile := "data/staging/config.json"}} // want to remove this 

    {{ $config := getJSON $pathToFile }}

Any help greatly appreciated :slight_smile:

1 Like

“When in doubt, add some parens.” - Rob Pike.

No, I don’t think he ever said that, but this works for me:

{{ $output := ("output" | printf "%s" | printf "%q")}}
{{ $output }}
2 Likes

BTW: I would rather use the “Data Files” feature for this:

{{ $config :=  (  (index .Site.Data  (getenv "TARGET_ENV")  )).config  }}

(the above is untested, but should work)

awesome thanks… i’ll try this out now