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
text/template: add "else with" like "else if" #57646
Comments
CC @robpike |
This adds no functionality, it is just a convenience. One may argue that it should be done for consistency, and it's not difficult although every change has costs (and for templates, they are doubled because of html/template). I am not convinced it is worth doing but I am not adamant. |
It's just a convenience for if/else too. This is real code:
Compare that to:
The difference might seem trivial if you're used to only touching templates occasionally or superficially, but it's a big quality of life improvement for those who spend hours writing non-trivial programs in templates, like authors of Hugo themes/modules. |
or you could format your existing code like this
|
It's not the shape of the code that's important, it's the complexity. |
@seankhliao there's no sane template formatter (e.g.: https://github.com/NiklasPor/prettier-plugin-go-template) that would format the template source the way you suggest, and throwing a way automatic template formatting is not something anyone would want to do. I agree with proposal. It should be of tremendous value (save lots of end user time) compared to the time spent on implementing it. I suspect that the people who say "thumbs down" to this are people who really don't spend any amount of time writing Go templates. |
Given the use cases presented here, this seems OK to do. |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Change https://go.dev/cl/545376 mentions this issue: |
This issue is currently labeled as early-in-cycle for Go 1.23. |
The
text/template
doc says:However, no such "else with" counterpart exists:
Which would be very useful:
Currently, we must do the nesting that the else/if doc mentions:
This gets tiresome.
Mixing the two would also be useful:
The text was updated successfully, but these errors were encountered: