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: (and html/template) execution option for given errors for missing map keys #6288
Comments
It's too close to make the 1.2 deadline but should happen for 1.3. Labels changed: added priority-later, feature, removed priority-triage. Owner changed to @robpike. Status changed to Accepted. |
A more complete explanation of my first comment: Execute can fall back to using the Getter interface if data is a struct and does not contain the field. So {{.Foo}} would resolve to data.Foo if defined and fall back to data.Get("Foo") if implemented. This enables custom handling in the Get method and the ability to return an error like method calls. This _could_ remove the need to implement a separate ExecuteStrict for stricter handling although I believe the default behavior should (eventually) be to return an error. The Getter support could just be another feature of the template engine. |
Comment 10 by jd@tekii.com.ar: There is a possible workaround this, and that is to define your parameter as an interface and then size the length to see that you are not getting any, and that means that you got no result from your map. http://play.golang.org/p/Kj7iqnqu1x |
Comment 13 by einthusan@wojka.com: Hi, I am having the same issue. I guess this didn't make it for Go 1.4 either... really need a way for template to fail upon missing map keys. |
You can implement pretty decent fail and default value behaviors with functions: |
@robpike What's the rationale of this string-based approach as opposed to Options field in template.Template? |
It's a simpler API this way and html/template will not need to echo every type and constant. |
by bjruth:
The text was updated successfully, but these errors were encountered: