Gin: How go-bindata supports the template ?

Created on 14 Apr 2018  路  6Comments  路  Source: gin-gonic/gin

How go-bindata supports the template

question

Most helpful comment

go-bindata is abandoned and you should not use that. I prefer to use go-assets to build a single binary containing assets.

With this, a server can go with such code below.

package main

import (
    "html/template"
    "io/ioutil"
    "net/http"
    "strings"

    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.New()
    t, err := loadTemplate()
    if err != nil {
        panic(err)
    }
    r.SetHTMLTemplate(t)
    r.GET("/", func(c *gin.Context) {
        c.HTML(http.StatusOK, "/html/index.tmpl", gin.H{
            "Foo": "World",
        })
    })
    r.GET("/bar", func(c *gin.Context) {
        c.HTML(http.StatusOK, "/html/bar.tmpl", gin.H{
            "Bar": "World",
        })
    })
    r.Run(":8080")
}

func loadTemplate() (*template.Template, error) {
    t := template.New("")
    for name, file := range Assets.Files {
        if file.IsDir() || !strings.HasSuffix(name, ".tmpl") {
            continue
        }
        h, err := ioutil.ReadAll(file)
        if err != nil {
            return nil, err
        }
        t, err = t.New(name).Parse(string(h))
        if err != nil {
            return nil, err
        }
    }
    return t, nil
}

I wrote a complete example on a repository. See this.

https://github.com/delphinus/gin-assets-sample

All 6 comments

go-bindata is abandoned and you should not use that. I prefer to use go-assets to build a single binary containing assets.

With this, a server can go with such code below.

package main

import (
    "html/template"
    "io/ioutil"
    "net/http"
    "strings"

    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.New()
    t, err := loadTemplate()
    if err != nil {
        panic(err)
    }
    r.SetHTMLTemplate(t)
    r.GET("/", func(c *gin.Context) {
        c.HTML(http.StatusOK, "/html/index.tmpl", gin.H{
            "Foo": "World",
        })
    })
    r.GET("/bar", func(c *gin.Context) {
        c.HTML(http.StatusOK, "/html/bar.tmpl", gin.H{
            "Bar": "World",
        })
    })
    r.Run(":8080")
}

func loadTemplate() (*template.Template, error) {
    t := template.New("")
    for name, file := range Assets.Files {
        if file.IsDir() || !strings.HasSuffix(name, ".tmpl") {
            continue
        }
        h, err := ioutil.ReadAll(file)
        if err != nil {
            return nil, err
        }
        t, err = t.New(name).Parse(string(h))
        if err != nil {
            return nil, err
        }
    }
    return t, nil
}

I wrote a complete example on a repository. See this.

https://github.com/delphinus/gin-assets-sample

@delphinus Could you also add an example to git/examples folder and update the readme? Thanks.

I did 馃憤

@delphinus I will add another example with https://github.com/UnnoTed/fileb0x package.

I got it. I finished the PR, so you can merge or edit my code now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

olegsobchuk picture olegsobchuk  路  3Comments

frederikhors picture frederikhors  路  3Comments

rawoke083 picture rawoke083  路  3Comments

mdnight picture mdnight  路  3Comments

sofish picture sofish  路  3Comments