I have a need to capture the output of a {{template ..}} call to variable. If some functions were exposed via consul-template when the containing template is processed this might be possible.
Can you guys please consider adding such a feature?
http://stackoverflow.com/questions/40164896/capture-or-assign-golang-template-output-to-variable
Actually just support (exposed function) for creating a struct in the template and pass it around would be great. Then the main template can create a empty struct, assign it to whatever, then pass that as the argument to {{template [arg]}} and then reference whatever the nested template sets in the struct.
{{define "mytemplate" }}
{{ .mytemplateOutput := printf "hello %s" .input }}
{{end}}
{{ $myStruct := createStruct }}
{{ $myStruct.input := "world" }}
{{ template "mytemplate" $myStruct }}
{{ $myVar := $myStruct.mytemplateOutput }}
{{ $myVar }}
Something that implements methods close to Java's Map interface should do the trick.
Too bad there's no magic method todo $myStruct.input.
It has the potential of looking like this.
{{- define "deftmpl" -}}
{{ .Remove "a" }}
{{ .Put "one" 1 }}
{{ .Put "two" 2 }}
{{- end -}}
{{- /********************************* */ -}}
Hello Template
{{ $myStruct := NewMap }}
{{/* output empty */}}
{{ $myStruct }}
{{ $myStruct.Put "a" "abc" }} {{ $myStruct.Put "p" "pq" }} {{ $myStruct.Put "x" "xyz" }}
{{/* output apx */}}
{{ $myStruct }}
{{ template "deftmpl" $myStruct}}
{{/* output px12 */}}
{{ $myStruct }}
{{ $tmp := $myStruct.Get "two" }}
{{ $yourStruct := NewMap }}
{{ $yourStruct.Put "z" "DEF" }}
{{ $yourStruct.ClearReturnBlank }}
{{ $yourStruct.Put "a" "ABC" }} {{ $yourStruct.Put "c" $tmp }}
{{/* output ac */}}
{{ $yourStruct }}
{{ $yourStruct.PutAllReturnBlank $myStruct }}
{{/* output acpx12 */}}
{{ $yourStruct }}
quickly put together in https://github.com/ewah/consul-template
with General Map structure for returning values from define. #768
Just this simple thing will make this pretty versatile, a referencable struct
Most helpful comment
Just this simple thing will make this pretty versatile, a referencable struct