So I'm at that awkward place where I'm trying to pass context data over to a template, and there's not a whole lot of really great examples out there that don't involve passing a preexisting structure. The best I have come up with is something like the following:
templ.Execute(writer, map[string]interface{}{
"thingyA": ThingyA,
"thingyB": ThingyB,
})
Using the above, I can reference ThingyA
and all of its properties and methods in my template by using {{ .thingyA.[PROPERTY] }}
. So that's pretty good in itself, in that it allows me to define a context in a simple Django-ish fashion, without using an anonymous struct, which would be a bit clunky.
But I'm wondering if there is some way to define a new type Context
, so that I could simplify the above to the following:
templ.Execute(writer, Context{
"thingyA": ThingyA,
"thingyB": ThingyB,
})
That is, I would like to define a specific type Context
that always has the form map[string]interface{}
, and can be initialized in the same manner as a map. Any ideas on how to go about this?