第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

go.rice 將模板加載到 gin 中

go.rice 將模板加載到 gin 中

Go
拉風(fēng)的咖菲貓 2021-12-07 10:51:12
我有以下目錄布局$ ls templates/bar.html    foo.html我已經(jīng)運(yùn)行了以下命令$ rice embed-go我的代碼看起來(lái)像包主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")}我遇到的問(wèn)題是我可以很好地卷曲以下 URL$ curl 0:8080/barbar so much bar問(wèn)題是 /index url 不起作用,因?yàn)樗黃etHTMLTemplate正在覆蓋它。我想知道如何將多個(gè)加載的模板從 go.rice 創(chuàng)建的 bindata 文件傳遞到 gin 中。我收到以下錯(cuò)誤[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謝謝
查看完整描述

1 回答

?
慕桂英546537

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊

SetHTMLTemplate 每次在循環(huán)中調(diào)用時(shí)都會(huì)覆蓋模板。


看了下面的,你可以試試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()


查看完整回答
反對(duì) 回復(fù) 2021-12-07
  • 1 回答
  • 0 關(guān)注
  • 260 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)