Go pattern Passing data from one to another

I need to go to the template. I need to pass some value from one template to another. IE

Template A has two variables named and .Type. Values ​​are passed from go code using ctx.Data. Template A links Template B with

{{ template "B" . }}

      

But in Template B..Name and .Type do not have the same name. I cannot change references to Template B variables because Tempalte B is used directly with those variables. Template B looks like this.

Username : {{ .UserName }}
Type : {{ .UserType }}

      

Now my question is how to change the .Name in template A to .UserName?

{{ tempalte "B" .Name as .UserName }}

      

is there something like this ????

+3


source to share


1 answer


Unfortunately this is not possible in the current version of Go text / template or html / template package.

You will need to add "Name" and "UserName" to ctx.Data

the Go side to be available with these exact names in template B.



You can look at pongo2, Django 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

+1


source







All Articles