Skip to content
/ bindatafs Public

Compile QOR templates into binary with go-bindata

License

Notifications You must be signed in to change notification settings

qor/bindatafs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 

Repository files navigation

BindataFS

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

GoDoc

Install BindataFS

$ go get -u -f github.com/qor/bindatafs/...

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

$ bindatafs config/bindatafs

Usage

import "<your_project>/config/bindatafs"

func main() {
  assetFS := bindatafs.AssetFS

  // Register view paths into AssetFS
  assetFS.RegisterPath("<your_project>/app/views")
  assetFS.RegisterPath("<your_project>/vender/plugin/views")

  // Compile templates under registered view paths into binary
  assetFS.Compile()

  // 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
  adminAssetFS.RegisterPath("<your_project>/app/admin_views")

  // Access file that under registered views paths of current name space
  adminAssetFS.Asset("admin_view.tmpl")
}

Use BindataFS with QOR Admin

import "<your_project>/config/bindatafs"

func main() {
  Admin = admin.New(&qor.Config{DB: db.Publish.DraftDB()})
  Admin.SetAssetFS(bindatafs.AssetFS.NameSpace("admin"))
}

Use BindataFS with QOR Render

import  "github.com/qor/render"

func main() {
  View := render.New()
  View.SetAssetFS(assetFS.NameSpace("views"))
}

Use BindataFS with QOR Widget

import  "github.com/qor/widget"

func main() {
  Widgets := widget.New(&widget.Config{DB: db.DB})
	Widgets.SetAssetFS(assetFS.NameSpace("widgets"))
}

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)
  }
}

License

Released under the MIT License.

About

Compile QOR templates into binary with go-bindata

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages