英文:
How to pass data to template automatically in Go?
问题
现在我正在使用Gorilla context包在我的中间件和控制器中传递数据,但我想要的是直接将数据传递给我的Pongo2模板,这样我在控制器中就不必从Gorilla context中获取数据并手动传递给模板上下文。对于熟悉express.js的人来说,就像这样:
var user = {
name: "Name",
age: 0
}
response.locals = user
编辑:所以每个pongo2模板都需要访问一个User对象,现在我使用中间件从数据库中获取用户,并使用Gorilla context将数据传递给我的控制器,然后传递给每个控制器上的模板,但我想要的是在中间件中将User对象传递给模板,而不是使用Gorilla context。
func UserMiddleware(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
user := &User{} // user通常从数据库中获取
context.Set(req, "user", user)
next.ServeHTTP(res, req)
})
}
然后在我的请求处理程序中:
func Handler(res http.ResponseWriter, req *http.Request) {
tpl, _ := pongo2.FromFile("view/template.html")
user := context.Get(req, "user").(*User)
data := pongo2.Context{
"user": user,
}
out, _ := tpl.Execute(data)
res.Write([]byte(out))
}
对于所有的处理程序,我都必须像这样传递user给模板,但我想要的是从我的中间件中传递它,这样我就不必在每个处理程序中这样做。
英文:
Right now i am using Gorilla context package to pass data around in my middlewares & controllers, but what i want to do is pass the data directly to my Pongo2 template so later in my controller i don't have to get the data from the Gorilla context and manually pass it to the template context, for those of you familiar with express.js it would be like
var user = {
name: "Name",
age: 0
}
response.locals = user
Edit: So every pongo2 template needs access to a User object, right now i fetch the user from database using middleware and using Gorilla context pass the data to my controller, from there on to my template on each controller but what i want to do is pass the User object to template from my middleware instead of using Gorilla context.
func UserMiddleware(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
user := &User{} // user will normally be fetched from database
context.Set(req, "user", user)
next.ServeHTTP(res, req)
})
}
Then In My Request Handler
func Handler(res http.ResponseWriter, req *http.Request) {
tpl, _ := pongo2.FromFile("view/template.html")
user := context.Get(req, "user").(*User)
data := pongo2.Context{
"user": user,
}
out, _ := tpl.Execute(data)
res.Write([]byte(out))
}
For all of my handlers i have to pass in user to template like that, but i want to pass it in from my middleware so that i don't have to do it in each of my handlers.
答案1
得分: 1
请注意,以下是翻译好的内容:
使用MyExecute(req, tpl)
来替代tpl.Execute(data)
func MyExecute(req *http.Request, tpl TemplateSet) (string, error){
gorillaObj := context.GetAll(req)
pongoObj := make(map[string]interface{})
for key, value := range gorillaObj {
if str, ok := key.(string); ok{
pongoObj[str] = value
}
}
return tpl.Execute(pongo2.Context(pongoObj))
}
未经测试,但应该可以工作。
最大的问题是gorilla使用map[interface{}]interface{}
作为存储,而pongo使用map[string]interface{}
,请注意在gorilla上下文中不要使用非字符串作为键。
英文:
invoke MyExecute(req, tpl)
instead of tpl.Execute(data)
func MyExecute(req *http.Request, tpl TemplateSet) (string, error){
gorillaObj := context.GetAll(req)
pongoObj := make(map[string]interface{})
for key, value := range gorillaObj {
if str, ok := key.(string); ok{
pongoObj[str] = value
}
}
return tpl.Execute(pongo2.Context(pongoObj))
}
not tested, it should work.
the most problem is that gorilla use map[interface{}]interface{}
as store, but pongo use map[string]interface{}
, note not to use non-string as key in gorilla context.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论