Open Yiannis128 opened 3 months ago
Although Conetxt.Keys and gin.H are both map[string]any, Keys are only used during the lifetime of the request and should not be exported as a result. gin.H is a specific output structure with multi-type support.
I suggest you use the code below to process the results and do something with the output
r.GET("/", func(ctx *gin.Context) {
var mergeData gin.H
for k, v := range ctx.Keys {
mergedData[k] = v // Process merge data
}
ctx.HTML(http.StatusOK, "index.html", mergeData)
})
Maybe it would be worth updating the documentation to state that, since my first impression was that it was a way of setting messages to be sent from the middleware to the endpoint.
Description
If you have middleware (using
r.Use()
), you can set keys through the methodctx.Set(k, v)
which puts them intoctx.Keys
. This sets the expectation of when using:You expect that the map
gin.H
's entries will be applied on top ofctx.Keys
, however, it overwrites them. Here is my Use method:Here are some of my routes:
Route /
Route about
Inside each one I simply output the context using
{{.}}
, the context is as follows:CTX: map[SignedIn:true Title:My website]
CTX: map[]
Multitemplate
I am using the github.com/gin-contrib/multitemplate plugin as I need to use multiple templates, and it's recommended in the docs to use that. I am assuming that it's working as intended.
Environment