英文:
go: load templates based on error code
问题
我想根据HTTP错误代码执行不同的模板。
我考虑使用template.ParseFiles()
,但我不确定如何动态地实现这一点。实际上,ParseFiles()
可以加载一个字符串数组(文件名)。
我是否需要为每个文件单独调用ParseFiles()
,以便将其结果分配给映射中的错误代码?如果我将所有文件都ParseFiles()
到一个数组中,那么如何将每个模板分配给错误代码?
我希望避免在每个请求上都执行ParseFiles()
,我更希望在init()
中进行解析,并且仅在请求时执行Execute
。
以下是我目前的代码(尚未编译):
package main
import (
"os"
"html/template"
"net/http"
)
var templateMap map[int]*template.Template
func init() {
initErrHandling()
}
func initErrHandling() {
templateMap = make(map[int]*template.Template)
templateMap[0] = "generic.html" //默认模板
templateMap[400] = "generic.html"
templateMap[401] = "unauthorized.html"
templateMap[403] = "forbidden.html"
templateMap[404] = "notfound.html"
templateMap[422] = "invalidparams.html"
templateMap[500] = "generic.html"
template.ParseFiles() //一次性解析所有文件,或者逐个解析文件并将其分配给错误代码,例如 templateMap[404],_ = template.parseFiles("notfound.html")?
}
func getTemplate(code int) (*template.Template) {
if val, tmpl := templateMap[code]; tmpl {
return tmpl
} else {
return templateMap[0]
}
}
func showError(w http.ResponseWriter, code int) {
getTemplate(code).Execute(w)
}
func main() {
showError(os.Stdout, 400)
}
请注意,这只是一个示例代码,尚未编译。你需要根据你的实际需求进行适当的修改和调试。
英文:
I would like to Execute a different template based on a http error code.
I was thinking to use template.ParseFiles()
, but I am confused how to do this dynamically. In fact, ParseFiles()
can load an array of strings (filenames).
Do I have to ParseFiles()
for every file individually, in order to assign its result to an error code in a map? If I ParseFiles()
all into an array, how can I then assign each of the templates to the error code?
I would like to avoid having to ParseFiles()
on each request, I would prefer to do the parsing in init()
and Execute
only on request.
Here's what I have so far (does not compile yet):
package main
import (
"os"
"html/template"
"net/http"
)
var templateMap map[int]*template.Template
func init() {
initErrHandling()
}
func initErrHandling() {
templateMap = make(map[int]*template.Template)
templateMap[0] = "generic.html" //default
templateMap[400] = "generic.html"
templateMap[401] = "unauthorized.html"
templateMap[403] = "forbidden.html"
templateMap[404] = "notfound.html"
templateMap[422] = "invalidparams.html"
templateMap[500] = "generic.html"
template.ParseFiles() //parseFiles all files in one call, or parseFiles one by one and assign to error code, e.g. templateMap[404],_ = template.parseFiles("notfound.html")?
}
func getTemplate(code int) (*template.Template) {
if val, tmpl := templateMap[code]; tmpl {
return tmpl
} else {
return templateMap[0]
}
}
func showError(w http.ResponseWriter, code int) {
getTemplate(code).Execute(w)
}
func main() {
showError(os.Stdout, 400)
}
答案1
得分: 2
使用一个映射来记录文件名,使用第二个映射来记录解析后的模板:
func initErrHandling() { // 从init()函数中调用
fnames := map[int]string{
0: "generic.html", // 默认
400: "generic.html",
401: "unauthorized.html",
403: "forbidden.html",
404: "notfound.html",
422: "invalidparams.html",
500: "generic.html",
}
templateMap = make(map[int]*template.Template)
for code, fname := range fnames {
templateMap[code] = template.Must(template.ParseFiles(fname))
}
}
英文:
Use one map to record the file names and a second map for the parsed templates:
func initErrHandling() { // call from init()
fnames := map[int]string{
0: "generic.html", //default
400: "generic.html",
401: "unauthorized.html",
403: "forbidden.html",
404: "notfound.html",
422: "invalidparams.html",
500: "generic.html",
}
templateMap = make(map[int]*template.Template)
for code, fname := range fnames {
templateMap[code] = template.Must(template.ParseFiles(fname))
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论