Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed May 24, 2021
1 parent 72009da commit db71c9d
Show file tree
Hide file tree
Showing 43 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion common/tplfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TplFuncs() template.FuncMap {
}

func ParseFiles(files ...string) (*template.Template, error) {
if FileSystem != nil {
if !FileSystem.IsEmpty() {
return ParseFS(FileSystem, files...)
}
name := filepath.Base(files[0])
Expand Down
32 changes: 28 additions & 4 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,38 @@ var (
LabelFn = func(s string) string {
return s
}
FileSystem fs.FS
FileSystem FileSystems

//private
cachedTemplate = make(map[string]*template.Template)
cachedConfig = make(map[string]*config.Config)
lock = new(sync.RWMutex)
)

type FileSystems []fs.FS

func (f FileSystems) Open(name string) (file fs.File, err error) {
for _, i := range f {
file, err = i.Open(name)
if err == nil {
return
}
}
return
}

func (f FileSystems) Size() int {
return len(f)
}

func (f FileSystems) IsEmpty() bool {
return f.Size() == 0
}

func (f *FileSystems) Register(fileSystem fs.FS) {
*f = append(*f, fileSystem)
}

const (
PACKAGE_NAME = "github.com/coscms/forms"
)
Expand Down Expand Up @@ -101,7 +125,7 @@ func TmplDir(style string) (tmplDir string) {

// CreateUrl creates the complete url of the desired widget template
func CreateUrl(widget string) string {
if FileSystem != nil {
if !FileSystem.IsEmpty() {
fp, err := FileSystem.Open(widget)
if err == nil {
defer fp.Close()
Expand All @@ -112,7 +136,7 @@ func CreateUrl(widget string) string {
}
}
if !TmplExists(widget) {
return filepath.Join(os.Getenv("GOPATH"), "src", PACKAGE_NAME, widget)
return filepath.Join(os.Getenv("GOPATH"), "src", PACKAGE_NAME, `defaults`, widget)
}
return widget
}
Expand Down Expand Up @@ -201,7 +225,7 @@ func ParseTmpl(data interface{},
return err.Error()
}
}
if FileSystem != nil {
if !FileSystem.IsEmpty() {
tpl, err = c.ParseFS(FileSystem, tpls...)
} else {
tpl, err = c.ParseFiles(tpls...)
Expand Down
4 changes: 2 additions & 2 deletions templates.go → defaults/templates.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package forms
package defaults

import (
"embed"
Expand All @@ -10,5 +10,5 @@ import (
var templateFS embed.FS

func init() {
common.FileSystem = templateFS
common.FileSystem.Register(templateFS)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

. "github.com/coscms/forms"
_ "github.com/coscms/forms/defaults"
)

type Test struct {
Expand Down

0 comments on commit db71c9d

Please sign in to comment.