How to insert a variable into a string within a template? [SOLVED]

I want to let users defining a themeColor by setting .Site.Params.themeColor to the wished color. The variable acts as prefix for the name of the stylesheets’ path (like <themeColor>-theme.html).

Firstly, I tried to use printf with the + operator:

{{ parial "themes/" + (printf "%s" .Site.Params.themeColor) + "-theme.html" }}

It looks like the + operator doesn’t works as expected, since Go doesn’t seem to support concatenation this way. My second approach was to insert the variable directly into the string, but it will not be interpreted:

{{ parial "themes/{{ .Site.Params.themeColor }}-theme.html" }}

Is there any workaround for that?

There is a spelling mistake above, and it looks like you are doing guesswork with your syntax. Reading the docs for printf would be a start:


{{ partial (printf "themes/%s-theme.html" $myvar)  }}
1 Like

I’ve corrected the spelling mistake and your solution should have been obvious. Thanks for the correction.