Reputation: 3163
I have to Go Template file. I need to pass some value from one template to another. IE
Template A have two variable with .Name and .Type. The values are passing from go code with ctx.Data. Template A references Template B with
{{ template "B" . }}
But in Template B. .Name and .Type are not having the same name. I can not change Template B s variable references because Tempalte B is used directly with those variable. Template B looks like this.
Username : {{ .UserName }}
Type : {{ .UserType }}
Now my question is how can i change the .Name in template A to .UserName?
{{ tempalte "B" .Name as .UserName }}
is there something like this????
Upvotes: 4
Views: 646
Reputation: 5500
Unfortunately, this is not possible with current version of Go's text/template or html/template package.
You would need to append "Name" and "UserName" to ctx.Data
on the Go side for them to be accessible with those exact names in template B.
You may want to look into pongo2, a Django-like template for Go (https://github.com/flosch/pongo2)
Your specific use case in pongo2 is documented here: https://github.com/flosch/pongo2/blob/master/template_tests/includes.tpl
Upvotes: 1