-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: text/template: slice spread functionality in template func context #51674
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
Comments
CC @robpike |
I'm not quite sure what is being asked. Is the "spread" operator the "..." token? A complete example that demonstrates the problem and what a solution might be would help. If the request is to add "..." to the template language, I don't think this is necessary or desirable. It's easy enough, as offered already, to do this with a custom function. |
My particular use case is in calling methods that use an "options" pattern that parses variadic function arguments to establish the method's options struct. In certain cases, when a pattern of similar usage emerges I have found a value in establishing reusable slices containing a grouping of options. type objectOptions struct {
...
}
func (o Object) Procedure(opts ...string) string{
var options objectOptions
// parse opts -> options
...
}
func (o Object) CommonBehavior() []string {
return []string{"option1", "option2", "option3"}
}
obj := Object{}
tmpl, _ := template.New("test").Parse(`
Ad Hoc Procedure: {{.Procedure "option1" "option2"}}
Repeated Procedure: {{.Procedure (spread .CommonBehavior)}}`)
_ = tmpl.Execute(os.Stdout, obj) The addition of the above as a built in template func could serve to bridge a gap between the template context and go context. Thank you for considering this proposal. |
Just have Procedure (or a variant) take a slice. The variadic touch here is unnecessary. Here's the variant:
That's so easy I don't see the need for a new feature in the template libraries. And since you already wrote essentially that in your original proposal, it's doubly clear that there is no semantic gap here. This is not a workaround, it's idiomatic Go. |
This proposal has been declined as retracted. |
Problem: Using variadic functions within text/templates there presently not a clean way to drop a defined slice into that function. Attempts to do so result in:
Direct usage of the spread operator valid in a go code context leads to a syntax error within a template context:
I can imagine two solutions to this problem. Either a "spread" builtin template function that can be dropped ahead of the struct to "spread" the a generic slice in a manner identical to the ellipsis notation but valid within a template context, or automatic type flexibility that allows variadic template funcs to process slices of the same type T as though they were valid variadic types.
Present Workaround:
Presently the only way I am aware of to work around this problem is to create a func wrapper.
If there are better workarounds, let me know. Otherwise, my humble thanks for considering this feature request.
The text was updated successfully, but these errors were encountered: