go.rice将模板加载到gin中
我有下面的目录布局go.rice将模板加载到gin中,go,Go,我有下面的目录布局 $ ls templates/ bar.html foo.html 我已经运行了以下命令 $ rice embed-go 我的代码看起来像 包干管 import ( "github.com/gin-gonic/gin" "github.com/GeertJohan/go.rice" "fmt" "html/template" ) func main() { router := gin.Default() //html := templa
$ ls templates/
bar.html foo.html
我已经运行了以下命令
$ rice embed-go
我的代码看起来像
包干管
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")
}
我遇到的问题是我可以很好地卷曲下面的URL
$ curl 0:8080/bar
bar so much bar
问题是/index url不工作,因为SetHTMLTemplate
正在覆盖它
我想知道如何将多个加载的模板从go.rice创建的bindata文件传递到gin中
我得到以下错误
[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
谢谢
SetHTMLTemplate
将在每次循环中调用模板时覆盖该模板
查看以下内容后,您可以尝试:
然后在路线定义中:
router.HTMLRender = loadTemplates()
router.HTMLRender = loadTemplates()