You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I needed to use macaron.Render with fileb0x instead of bindata. I think that this code will be helpful for anyone who might need to use vfsgen, packr, statik, rice and other bindata-style systems.
package router
import (
"bytes""fmt""io""path/filepath""code.example.com/me/go-static""gopkg.in/macaron.v1"
)
// Replace the one from macaron.Render since the files field is private// TplFileSystem implements TemplateFileSystem interface.typeTplFileSystemstruct {
files []macaron.TemplateFile
}
// NewTemplateFileSystem creates new template file system with given options.funcNewTemplateFileSystem(lastDirstring, exts...string) macaron.TemplateFileSystem {
fs:=TplFileSystem{}
fs.files=make([]macaron.TemplateFile, 0, 10)
if0==len(exts) {
exts= []string{".tmpl", ".html"}
}
// This is the fileb0x WalkDirspaths, _:=static.WalkDirs(lastDir, false)
for_, path:=rangepaths {
r, err:=filepath.Rel(lastDir, path)
ext:=macaron.GetExt(r)
for_, extension:=rangeexts {
ifext!=extension {
continue
}
vardata []byte// Loop over candidates of directory, break out once found.// The file always exists because it's inside the walk function,// and read original file is the worst case.path=filepath.Join(lastDir, r)
data, err=static.ReadFile(path)
iferr!=nil {
panic(err)
}
name:=filepath.ToSlash((r[0 : len(r)-len(ext)]))
fs.files=append(fs.files, macaron.NewTplFile(name, data, ext))
}
}
returnfs
}
func (fsTplFileSystem) ListFiles() []macaron.TemplateFile {
returnfs.files
}
func (fsTplFileSystem) Get(namestring) (io.Reader, error) {
fori:=rangefs.files {
iffs.files[i].Name()+fs.files[i].Ext() ==name {
returnbytes.NewReader(fs.files[i].Data()), nil
}
}
returnnil, fmt.Errorf("file '%s' not found", name)
}
This could probably be updated to use a generic interface that would work with many different FileSystem implementations and be created as a PR. However, for now I'll just leave this an example that others can easily follow.
The text was updated successfully, but these errors were encountered:
I needed to use
macaron.Render
withfileb0x
instead ofbindata
. I think that this code will be helpful for anyone who might need to usevfsgen
,packr
,statik
,rice
and other bindata-style systems.This could probably be updated to use a generic interface that would work with many different FileSystem implementations and be created as a PR. However, for now I'll just leave this an example that others can easily follow.
The text was updated successfully, but these errors were encountered: