Within a template, how can I achieve this?
{{$var := template "my-template"}}
I just get "unexpected <template> in operand"
.
Within a template, how can I achieve this?
{{$var := template "my-template"}}
I just get "unexpected <template> in operand"
.
There is no "builtin" action for getting the result of a template execution, but you may do it by registering a function which does that.
You can register functions with the
Template.Funcs()
function, you may execute a named template withTemplate.ExecuteTemplate()
and you may use abytes.Buffer
as the target (direct template execution result into a buffer).Here is a complete example:
Output (try it on the Go Playground):
The
"my-template"
template is executed by the registered functionexecTempl()
, and the result is returned as astring
, which is stored in the$var
template variable, which then is simply added to the output, but you may use it to pass to other functions if you want to.