doushun1870 2016-01-17 15:27
浏览 190
已采纳

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

  • 写回答

1条回答 默认 最新

  • doujiangao4229 2016-01-18 15:07
    关注

    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()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c