BindataFS could be used to compile templates into binary utilizing go-bindata


Install BindataFS

$ go get -u -f

Initialize BindataFS for your project, set the path you want to store BindataFS related files, e.g. config/bindatafs:

$ bindatafs config/bindatafs


import "<your_project>/config/bindatafs"

func main() {
  assetFS := bindatafs.AssetFS

  // Register view paths into AssetFS

  // Compile templates under registered view paths into binary

  // Get file content with name
  fileContent, ok := assetFS.Asset("home/index.tmpl")

You need to compile templates into a go file with method Compile before run go build, and if any templates changed, you need to recompile it.

If you started your application with tag bindatafs, AssetFS will access file from generated go file or current binary

go run -tags 'bindatafs' main.go

Or it will access its content from registered view paths of your filesystem, which is easier for your local development

go run main.go

Using NameSpace

Although you could initalize several assetfs packages to hold templates from different view paths (templates name might has confliction) for different use cases, Bindatafs provide an easier solution for you.

func main() {
  // Generate a sub AssetFS with namespace
  adminAssetFS := assetFS.NameSpace("admin_related_files")

  // Register view paths into this name space

  // Access file that under registered views paths of current name space

Use BindataFS with QOR Admin

import "<your_project>/config/bindatafs"

func main() {
  Admin = admin.New(&qor.Config{DB: db.Publish.DraftDB()})

Use BindataFS with QOR Render

import  ""

func main() {
  View := render.New()

Use BindataFS with QOR Widget

import  ""

func main() {
  Widgets := widget.New(&widget.Config{DB: db.DB})

Use BindataFS with static files

func main() {
  mux := http.NewServeMux()

  // this will add all files under public into a generated go file, which will be included into the binary
  assetFS := assetFS.FileServer(http.Dir("public"))

  // If you only want to include specified paths, you could use it like this
  assetFS := assetFS.FileServer(http.Dir("public"), "javascripts", "stylesheets", "images")

  for _, path := range []string{"javascripts", "stylesheets", "images"} {
    mux.Handle(fmt.Sprintf("/%s/", path), assetFS)

results matching ""

    No results matching ""