英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论