-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: text/template: struct tags #47663
Comments
cc @robpike |
I'm not sure what this tag means. Can you give an example of how this would be used? Thanks. |
Sure. So the File struct (as an example) has a field called URL, but when it is being used in a template, it transforms into type File struct {
URL string `json:"url" template:"example"`
Name string `json:"name" template: "-"`
}
func Test() error {
str := "{{ .Example }}"
tpl, err := template.New("").Parse(str)
if err != nil {
return err
}
f := File{
URL: "myurl",
Name: "name", // Private, no one can access it when executing a tpl.
}
buf := bytes.Buffer{}
err = tpl.Execute(&buf, f)
if err != nil {
return err
}
// Would output `myurl`
return nil
} Hope that makes sense. |
I'm sorry, I don't understand why this is useful. Templates aren't like JSON, where Go code has to conform to externally defined data formats that were not designed with Go in mind. Templates are only used with Go code. Why do we need a translation layer from Go code to Go code? |
I can see where you are coming from but more often that not when you are exporting information to a template 9 times out of 10 you have to transform it in one way or another. |
I don't understand why that would be true. Can you point to a couple of examples in existing open source packages? Thanks. |
Nitpick: type File struct {
URL string `json:"url" template:"example"`
Name string `json:"name" template: "-"`
}
func Test() error {
str := "{{ .Example }}" Surely this should be This proposal makes the most sense with user provided or otherwise external templates, but in general templates are taken from trusted sources, so it's little bit of an odd usecase. I bet you could write something that uses package reflect to create a map[string]interface{} based on the struct tags and use it like this: f := File{
URL: "myurl",
Name: "name", // Private, no one can access it when executing a tpl.
}
buf := bytes.Buffer{}
err = tpl.Execute(&buf, MyConverter(f)) |
This proposal has been added to the active column of the proposals project |
I do not understand the purpose of this proposal. The programmer is already in control of the names in the struct and in the template and there is no information leak that needs to be shut down. |
Based on the discussion above, this proposal seems like a likely decline. |
No worries thanks for considering |
No change in consensus, so declined. |
Proposal:
It would be great if we could assign template tags to a struct so we can hide them from public view or rewrite the name when executing a template.
On many occasions we have to create a
Public
method. Having a tag similar tojson
on the struct would make so much sense.I'm not sure if this has been discussed before but an example is below:
Example
Thank you.
The text was updated successfully, but these errors were encountered: