goview icon indicating copy to clipboard operation
goview copied to clipboard

Embeded Static file support on golang 1.16 standard Example

Open golfapipol opened this issue 4 years ago • 1 comments

I see issue #9 that you have support on go.rice https://github.com/foolin/goview/issues/9 do you have plan to support go embeded or do you have example to do it? https://golang.org/pkg/embed/

golfapipol avatar May 04 '21 10:05 golfapipol

If you've a file layout with like, ui/index.html and ui/whatever.html, you can do this:

//go:embed ui/*
var ui embed.FS

func embeddedFH(config goview.Config, tmpl string) (string, error) {
	path := filepath.Join(config.Root, tmpl)
	bytes, err := ui.ReadFile(path + config.Extension)
	return string(bytes), err
}

func main() {
	//new template engine
	gv := ginview.New(
		goview.Config{
			Root:         "ui",
			Extension:    ".html",
		},
	)
	gv.ViewEngine.SetFileHandler(embeddedFH)
	router.HTMLRender = gv
}

And it'll render from your embedded assets.

terrbear avatar Jul 09 '21 03:07 terrbear