go.rice将模板加载到杜松子酒

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

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