Home > Software engineering >  Go Nested Template path as a variable
Go Nested Template path as a variable

Time:09-17

This is really simple, but I'm a Go newb and I can't seem to find the documentation for how I might use a variable interpolation in an nested / associated template function.

Here's the file.tmpl I'm trying to include "/path/to/backend.txt" from.

blah
{{template $.Backends .}}
blah

In the above, the variable $Backends exists and is a string eg. "/path/to/backends.txt". I'm hoping to simply interpolate it into the template function, but Go is not happy with it. Throwing unexpected "$" in template clause.

Any suggestions on how this is done, escaped, or am I totally abusing the whole template like this?

Thanks.

CodePudding user response:

The argument to "template" cannot be a variable. Note that "template" is not a function, but a template action. So you can only use a constant to instantiate a template.

One reason this is not allowed is security. If a variable was allowed, a vulnerable application would allow random files on the file system to be included in the rendered template body.

  • Related