Closed gopherbot closed 9 years ago
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
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? If new options are added you will have to update text/template and html/template anyway. Struct-based implementation would add compile-time safety and feels cleaner.
It's a simpler API this way and html/template will not need to echo every type and constant.
by bjruth: