Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How go-bindata supports the template ? #1316

Closed
jicg opened this issue Apr 14, 2018 · 6 comments
Labels

Comments

@jicg
Copy link

@jicg jicg commented Apr 14, 2018

How go-bindata supports the template

@jicg jicg changed the title How go-bind supports the template ? How go-bindata supports the template ? Apr 14, 2018
@delphinus

This comment has been minimized.

Copy link

@delphinus delphinus commented Apr 22, 2018

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

@appleboy

This comment has been minimized.

Copy link
Member

@appleboy appleboy commented Apr 22, 2018

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

@delphinus

This comment has been minimized.

Copy link

@delphinus delphinus commented Apr 22, 2018

I did 👍

@appleboy

This comment has been minimized.

Copy link
Member

@appleboy appleboy commented Apr 22, 2018

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

@delphinus

This comment has been minimized.

Copy link

@delphinus delphinus commented Apr 22, 2018

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

@appleboy

This comment has been minimized.

Copy link
Member

@appleboy appleboy commented Apr 22, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.