Templates aren't really request scopedĀ though, right? I read the term
"request scoped" as "things like request IDs, timing information and
maybe user authentication".
What I usually do in situations like this is to make the handler
functions methods of a `RequestHandler` type which in your case would be
the `ApplicationEngine`. Then you create an instance of that struct in
your `main` or wherever and register its methods as HTTP handler
functions (or whatever protocol you use for transport).
On 21.08.20 00:36, Ibraheem Ahmed wrote:
In my application I store a map of all my templates
(`map[string]*template.Template`) in an `ApplicationEngine` struct,
which is my application framework instance. I am currently using the
standard `context.Context` to pass request scoped values between my
middlewares and handlers. I need a way to access that map of templates
in order to render a specific template file. What is the best way to
do this? Is storing a map of templates even a good idea?
I know that contexts are a controversial issue and just want to hear
the available options.
I am currently thinking of adopting a way similar to the [gin
framework's
context](https://github.com/gin-gonic/gin/blob/master/context.go#L56).
Gin uses a custom struct that stores a pointer to `gin.Engine`, which
is their application framework instance. They then have a `HTML()`
function on the context struct, which accesses the template map stored
in the `gin.Engine` pointer.
While my question is specific to my need, there is a broader
discussion here. Should you use custom structs instead of the
"standard" `context.Context`? Most web frameworks (gobuffalo, gin,
echo) seem to handle it this way, but there seems to be no consensus
on if it is a good idea.
--
You received this message because you are subscribed to the Google
Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to golang-nuts+unsubscr...@googlegroups.com
<mailto:golang-nuts+unsubscr...@googlegroups.com>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/969f4dc0-f63a-4a9f-b8ff-04457498107en%40googlegroups.com
<https://groups.google.com/d/msgid/golang-nuts/969f4dc0-f63a-4a9f-b8ff-04457498107en%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
--
Gregor Best
b...@pferdewetten.de
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/114994e2-991f-6203-053e-ff6ff79937ef%40pferdewetten.de.