Skip to content

Commit db71c9d

Browse files
committed
update
1 parent 72009da commit db71c9d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+32
-7
lines changed

common/tplfunc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TplFuncs() template.FuncMap {
3333
}
3434

3535
func ParseFiles(files ...string) (*template.Template, error) {
36-
if FileSystem != nil {
36+
if !FileSystem.IsEmpty() {
3737
return ParseFS(FileSystem, files...)
3838
}
3939
name := filepath.Base(files[0])

common/types.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,38 @@ var (
4646
LabelFn = func(s string) string {
4747
return s
4848
}
49-
FileSystem fs.FS
49+
FileSystem FileSystems
5050

5151
//private
5252
cachedTemplate = make(map[string]*template.Template)
5353
cachedConfig = make(map[string]*config.Config)
5454
lock = new(sync.RWMutex)
5555
)
5656

57+
type FileSystems []fs.FS
58+
59+
func (f FileSystems) Open(name string) (file fs.File, err error) {
60+
for _, i := range f {
61+
file, err = i.Open(name)
62+
if err == nil {
63+
return
64+
}
65+
}
66+
return
67+
}
68+
69+
func (f FileSystems) Size() int {
70+
return len(f)
71+
}
72+
73+
func (f FileSystems) IsEmpty() bool {
74+
return f.Size() == 0
75+
}
76+
77+
func (f *FileSystems) Register(fileSystem fs.FS) {
78+
*f = append(*f, fileSystem)
79+
}
80+
5781
const (
5882
PACKAGE_NAME = "github.com/coscms/forms"
5983
)
@@ -101,7 +125,7 @@ func TmplDir(style string) (tmplDir string) {
101125

102126
// CreateUrl creates the complete url of the desired widget template
103127
func CreateUrl(widget string) string {
104-
if FileSystem != nil {
128+
if !FileSystem.IsEmpty() {
105129
fp, err := FileSystem.Open(widget)
106130
if err == nil {
107131
defer fp.Close()
@@ -112,7 +136,7 @@ func CreateUrl(widget string) string {
112136
}
113137
}
114138
if !TmplExists(widget) {
115-
return filepath.Join(os.Getenv("GOPATH"), "src", PACKAGE_NAME, widget)
139+
return filepath.Join(os.Getenv("GOPATH"), "src", PACKAGE_NAME, `defaults`, widget)
116140
}
117141
return widget
118142
}
@@ -201,7 +225,7 @@ func ParseTmpl(data interface{},
201225
return err.Error()
202226
}
203227
}
204-
if FileSystem != nil {
228+
if !FileSystem.IsEmpty() {
205229
tpl, err = c.ParseFS(FileSystem, tpls...)
206230
} else {
207231
tpl, err = c.ParseFiles(tpls...)

templates.go renamed to defaults/templates.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package forms
1+
package defaults
22

33
import (
44
"embed"
@@ -10,5 +10,5 @@ import (
1010
var templateFS embed.FS
1111

1212
func init() {
13-
common.FileSystem = templateFS
13+
common.FileSystem.Register(templateFS)
1414
}
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.

example/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
. "github.com/coscms/forms"
9+
_ "github.com/coscms/forms/defaults"
910
)
1011

1112
type Test struct {

0 commit comments

Comments
 (0)