根据错误代码加载模板。

huangapple go评论114阅读模式
英文:

go: load templates based on error code

问题

我想根据HTTP错误代码执行不同的模板。

我考虑使用template.ParseFiles(),但我不确定如何动态地实现这一点。实际上,ParseFiles()可以加载一个字符串数组(文件名)。

我是否需要为每个文件单独调用ParseFiles(),以便将其结果分配给映射中的错误代码?如果我将所有文件都ParseFiles()到一个数组中,那么如何将每个模板分配给错误代码?

我希望避免在每个请求上都执行ParseFiles(),我更希望在init()中进行解析,并且仅在请求时执行Execute

以下是我目前的代码(尚未编译):

  1. package main
  2. import (
  3. "os"
  4. "html/template"
  5. "net/http"
  6. )
  7. var templateMap map[int]*template.Template
  8. func init() {
  9. initErrHandling()
  10. }
  11. func initErrHandling() {
  12. templateMap = make(map[int]*template.Template)
  13. templateMap[0] = "generic.html" //默认模板
  14. templateMap[400] = "generic.html"
  15. templateMap[401] = "unauthorized.html"
  16. templateMap[403] = "forbidden.html"
  17. templateMap[404] = "notfound.html"
  18. templateMap[422] = "invalidparams.html"
  19. templateMap[500] = "generic.html"
  20. template.ParseFiles() //一次性解析所有文件,或者逐个解析文件并将其分配给错误代码,例如 templateMap[404],_ = template.parseFiles("notfound.html")?
  21. }
  22. func getTemplate(code int) (*template.Template) {
  23. if val, tmpl := templateMap[code]; tmpl {
  24. return tmpl
  25. } else {
  26. return templateMap[0]
  27. }
  28. }
  29. func showError(w http.ResponseWriter, code int) {
  30. getTemplate(code).Execute(w)
  31. }
  32. func main() {
  33. showError(os.Stdout, 400)
  34. }

请注意,这只是一个示例代码,尚未编译。你需要根据你的实际需求进行适当的修改和调试。

英文:

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):

  1. package main
  2. import (
  3. "os"
  4. "html/template"
  5. "net/http"
  6. )
  7. var templateMap map[int]*template.Template
  8. func init() {
  9. initErrHandling()
  10. }
  11. func initErrHandling() {
  12. templateMap = make(map[int]*template.Template)
  13. templateMap[0] = "generic.html" //default
  14. templateMap[400] = "generic.html"
  15. templateMap[401] = "unauthorized.html"
  16. templateMap[403] = "forbidden.html"
  17. templateMap[404] = "notfound.html"
  18. templateMap[422] = "invalidparams.html"
  19. templateMap[500] = "generic.html"
  20. 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")?
  21. }
  22. func getTemplate(code int) (*template.Template) {
  23. if val, tmpl := templateMap[code]; tmpl {
  24. return tmpl
  25. } else {
  26. return templateMap[0]
  27. }
  28. }
  29. func showError(w http.ResponseWriter, code int) {
  30. getTemplate(code).Execute(w)
  31. }
  32. func main() {
  33. showError(os.Stdout, 400)
  34. }

答案1

得分: 2

使用一个映射来记录文件名,使用第二个映射来记录解析后的模板:

  1. func initErrHandling() { // 从init()函数中调用
  2. fnames := map[int]string{
  3. 0: "generic.html", // 默认
  4. 400: "generic.html",
  5. 401: "unauthorized.html",
  6. 403: "forbidden.html",
  7. 404: "notfound.html",
  8. 422: "invalidparams.html",
  9. 500: "generic.html",
  10. }
  11. templateMap = make(map[int]*template.Template)
  12. for code, fname := range fnames {
  13. templateMap[code] = template.Must(template.ParseFiles(fname))
  14. }
  15. }
英文:

Use one map to record the file names and a second map for the parsed templates:

  1. func initErrHandling() { // call from init()
  2. fnames := map[int]string{
  3. 0: "generic.html", //default
  4. 400: "generic.html",
  5. 401: "unauthorized.html",
  6. 403: "forbidden.html",
  7. 404: "notfound.html",
  8. 422: "invalidparams.html",
  9. 500: "generic.html",
  10. }
  11. templateMap = make(map[int]*template.Template)
  12. for code, fname := range fnames {
  13. templateMap[code] = template.Must(template.ParseFiles(fname))
  14. }
  15. }

huangapple
  • 本文由 发表于 2017年3月11日 04:07:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/42726871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定