使用go.rice将模板加载到gin中。

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

go.rice load templates into gin

问题

我有以下的目录布局

$ ls templates/
bar.html	foo.html

我运行了以下的命令

$ rice embed-go

我的代码如下

package main

import (
  "github.com/gin-gonic/gin"
  "github.com/GeertJohan/go.rice"
  "fmt"
  "html/template"
)


func main() {
  router := gin.Default()

  //html := template.Must(template.ParseFiles("templates/foo.html", "templates/bar.html"))
  //router.SetHTMLTemplate(html)

  templateBox, err := rice.FindBox("templates")
  if err != nil {
      fmt.Println(err)
  }

  list := [...]string{"foo.html", "bar.html"}
  for _, x := range list {
    templateString, err := templateBox.String(x)
    if err != nil {
        fmt.Println(err)
    }

    tmplMessage, err := template.New(x).Parse(templateString)
    if err != nil {
        fmt.Println(err)
    }

    router.SetHTMLTemplate(tmplMessage)
  }


  router.GET("/index", func(c *gin.Context) {
      c.HTML(200, "foo.html", gin.H{
          "Message": "主页",
      })
  })
  router.GET("/bar", func(c *gin.Context) {
      c.HTML(200, "bar.html", gin.H{
          "Message": "很多bar",
      })
  })
  router.Run(":8080")
}

我遇到的问题是,我可以正常使用curl访问以下URL

$ curl 0:8080/bar
bar so much bar

问题是/index的URL无法工作,因为SetHTMLTemplate覆盖了它。

我想知道如何将由go.rice创建的多个加载的模板传递给gin。

我得到了以下错误

[GIN-debug] [ERROR] html/template: "foo.html"未定义
[GIN] 2016/01/17 - 07:19:40 | 500 |      67.033µs | 127.0.0.1:52467 |   GET     /index

谢谢

英文:

I have the following dir layout

$ ls templates/
bar.html	foo.html

I have run the following command

$ rice embed-go

My code looks like

package main

import (
  "github.com/gin-gonic/gin"
  "github.com/GeertJohan/go.rice"
  "fmt"
  "html/template"
)


func main() {
  router := gin.Default()

  //html := template.Must(template.ParseFiles("templates/foo.html", "templates/bar.html"))
  //router.SetHTMLTemplate(html)

  templateBox, err := rice.FindBox("templates")
  if err != nil {
      fmt.Println(err)
  }

  list := [...]string{"foo.html", "bar.html"}
  for _, x := range list {
    templateString, err := templateBox.String(x)
    if err != nil {
        fmt.Println(err)
    }

    tmplMessage, err := template.New(x).Parse(templateString)
    if err != nil {
        fmt.Println(err)
    }

    router.SetHTMLTemplate(tmplMessage)
  }


  router.GET("/index", func(c *gin.Context) {
      c.HTML(200, "foo.html", gin.H{
          "Message": "Main website",
      })
  })
  router.GET("/bar", func(c *gin.Context) {
      c.HTML(200, "bar.html", gin.H{
          "Message": "so much bar",
      })
  })
  router.Run(":8080")
}

The issue I am having is I can curl the following URL just fine

$ curl 0:8080/bar
bar so much bar

The issue is the /index url isn't working because the SetHTMLTemplate is overwriting it.

I'm wondering how I can pass multiple loaded templates from a bindata file created by go.rice into gin.

I get the following error

[GIN-debug] [ERROR] html/template: "foo.html" is undefined
[GIN] 2016/01/17 - 07:19:40 | 500 |      67.033µs | 127.0.0.1:52467 |   GET     /index

Thanks

答案1

得分: 3

SetHTMLTemplate在循环中每次调用时都会覆盖模板。

在查看以下内容后,您可以尝试访问https://github.com/gin-gonic/gin/issues/320:

func loadTemplates() multitemplate.Render {
  templateBox, err := rice.FindBox("templates")
  if err != nil {
      fmt.Println(err)
  }

  r := multitemplate.New()

  list := [...]string{"foo.html", "bar.html"}
  for _, x := range list {
    templateString, err := templateBox.String(x)
    if err != nil {
        fmt.Println(err)
    }

    tmplMessage, err := template.New(x).Parse(templateString)
    if err != nil {
        fmt.Println(err)
    }

    r.Add(x, tmplMessage)
  }

  return r
}

然后在您的路由定义中

router.HTMLRender = loadTemplates()
英文:

SetHTMLTemplate will override the template every time it's called in the loop.

After looking at the following, you can try https://github.com/gin-gonic/gin/issues/320:

func loadTemplates() multitemplate.Render {
  templateBox, err := rice.FindBox("templates")
  if err != nil {
      fmt.Println(err)
  }

  r := multitemplate.New()

  list := [...]string{"foo.html", "bar.html"}
  for _, x := range list {
    templateString, err := templateBox.String(x)
    if err != nil {
        fmt.Println(err)
    }

    tmplMessage, err := template.New(x).Parse(templateString)
    if err != nil {
        fmt.Println(err)
    }

    r.Add(x, tmplMessage)
  }

  return r
}

Then in your route definition:

router.HTMLRender = loadTemplates()

huangapple
  • 本文由 发表于 2016年1月17日 23:27:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/34840050.html
匿名

发表评论

匿名网友

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

确定