diff --git a/common/tplfunc.go b/common/tplfunc.go index b33e19d..5213715 100644 --- a/common/tplfunc.go +++ b/common/tplfunc.go @@ -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]) diff --git a/common/types.go b/common/types.go index 0a0fa96..d6a92a6 100644 --- a/common/types.go +++ b/common/types.go @@ -46,7 +46,7 @@ var ( LabelFn = func(s string) string { return s } - FileSystem fs.FS + FileSystem FileSystems //private cachedTemplate = make(map[string]*template.Template) @@ -54,6 +54,30 @@ var ( 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" ) @@ -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() @@ -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 } @@ -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...) diff --git a/templates.go b/defaults/templates.go similarity index 67% rename from templates.go rename to defaults/templates.go index 7b6cc9e..17ac602 100644 --- a/templates.go +++ b/defaults/templates.go @@ -1,4 +1,4 @@ -package forms +package defaults import ( "embed" @@ -10,5 +10,5 @@ import ( var templateFS embed.FS func init() { - common.FileSystem = templateFS + common.FileSystem.Register(templateFS) } diff --git a/templates/allfields.html b/defaults/templates/allfields.html similarity index 100% rename from templates/allfields.html rename to defaults/templates/allfields.html diff --git a/templates/base/button.html b/defaults/templates/base/button.html similarity index 100% rename from templates/base/button.html rename to defaults/templates/base/button.html diff --git a/templates/base/datetime/date.html b/defaults/templates/base/datetime/date.html similarity index 100% rename from templates/base/datetime/date.html rename to defaults/templates/base/datetime/date.html diff --git a/templates/base/datetime/datetime.html b/defaults/templates/base/datetime/datetime.html similarity index 100% rename from templates/base/datetime/datetime.html rename to defaults/templates/base/datetime/datetime.html diff --git a/templates/base/datetime/time.html b/defaults/templates/base/datetime/time.html similarity index 100% rename from templates/base/datetime/time.html rename to defaults/templates/base/datetime/time.html diff --git a/templates/base/fieldset.html b/defaults/templates/base/fieldset.html similarity index 100% rename from templates/base/fieldset.html rename to defaults/templates/base/fieldset.html diff --git a/templates/base/fieldset_buttons.html b/defaults/templates/base/fieldset_buttons.html similarity index 100% rename from templates/base/fieldset_buttons.html rename to defaults/templates/base/fieldset_buttons.html diff --git a/templates/base/generic.tmpl b/defaults/templates/base/generic.tmpl similarity index 100% rename from templates/base/generic.tmpl rename to defaults/templates/base/generic.tmpl diff --git a/templates/base/input.html b/defaults/templates/base/input.html similarity index 100% rename from templates/base/input.html rename to defaults/templates/base/input.html diff --git a/templates/base/langset.html b/defaults/templates/base/langset.html similarity index 100% rename from templates/base/langset.html rename to defaults/templates/base/langset.html diff --git a/templates/base/number/number.html b/defaults/templates/base/number/number.html similarity index 100% rename from templates/base/number/number.html rename to defaults/templates/base/number/number.html diff --git a/templates/base/number/range.html b/defaults/templates/base/number/range.html similarity index 100% rename from templates/base/number/range.html rename to defaults/templates/base/number/range.html diff --git a/templates/base/options/checkbox.html b/defaults/templates/base/options/checkbox.html similarity index 100% rename from templates/base/options/checkbox.html rename to defaults/templates/base/options/checkbox.html diff --git a/templates/base/options/radiobutton.html b/defaults/templates/base/options/radiobutton.html similarity index 100% rename from templates/base/options/radiobutton.html rename to defaults/templates/base/options/radiobutton.html diff --git a/templates/base/options/select.html b/defaults/templates/base/options/select.html similarity index 100% rename from templates/base/options/select.html rename to defaults/templates/base/options/select.html diff --git a/templates/base/static.html b/defaults/templates/base/static.html similarity index 100% rename from templates/base/static.html rename to defaults/templates/base/static.html diff --git a/templates/base/text/passwordinput.html b/defaults/templates/base/text/passwordinput.html similarity index 100% rename from templates/base/text/passwordinput.html rename to defaults/templates/base/text/passwordinput.html diff --git a/templates/base/text/textareainput.html b/defaults/templates/base/text/textareainput.html similarity index 100% rename from templates/base/text/textareainput.html rename to defaults/templates/base/text/textareainput.html diff --git a/templates/base/text/textinput.html b/defaults/templates/base/text/textinput.html similarity index 100% rename from templates/base/text/textinput.html rename to defaults/templates/base/text/textinput.html diff --git a/templates/baseform.html b/defaults/templates/baseform.html similarity index 100% rename from templates/baseform.html rename to defaults/templates/baseform.html diff --git a/templates/bootstrap3/button.html b/defaults/templates/bootstrap3/button.html similarity index 100% rename from templates/bootstrap3/button.html rename to defaults/templates/bootstrap3/button.html diff --git a/templates/bootstrap3/datetime/date.html b/defaults/templates/bootstrap3/datetime/date.html similarity index 100% rename from templates/bootstrap3/datetime/date.html rename to defaults/templates/bootstrap3/datetime/date.html diff --git a/templates/bootstrap3/datetime/datetime.html b/defaults/templates/bootstrap3/datetime/datetime.html similarity index 100% rename from templates/bootstrap3/datetime/datetime.html rename to defaults/templates/bootstrap3/datetime/datetime.html diff --git a/templates/bootstrap3/datetime/time.html b/defaults/templates/bootstrap3/datetime/time.html similarity index 100% rename from templates/bootstrap3/datetime/time.html rename to defaults/templates/bootstrap3/datetime/time.html diff --git a/templates/bootstrap3/fieldset.html b/defaults/templates/bootstrap3/fieldset.html similarity index 100% rename from templates/bootstrap3/fieldset.html rename to defaults/templates/bootstrap3/fieldset.html diff --git a/templates/bootstrap3/fieldset_buttons.html b/defaults/templates/bootstrap3/fieldset_buttons.html similarity index 100% rename from templates/bootstrap3/fieldset_buttons.html rename to defaults/templates/bootstrap3/fieldset_buttons.html diff --git a/templates/bootstrap3/generic.tmpl b/defaults/templates/bootstrap3/generic.tmpl similarity index 100% rename from templates/bootstrap3/generic.tmpl rename to defaults/templates/bootstrap3/generic.tmpl diff --git a/templates/bootstrap3/input.html b/defaults/templates/bootstrap3/input.html similarity index 100% rename from templates/bootstrap3/input.html rename to defaults/templates/bootstrap3/input.html diff --git a/templates/bootstrap3/langset.html b/defaults/templates/bootstrap3/langset.html similarity index 100% rename from templates/bootstrap3/langset.html rename to defaults/templates/bootstrap3/langset.html diff --git a/templates/bootstrap3/number/number.html b/defaults/templates/bootstrap3/number/number.html similarity index 100% rename from templates/bootstrap3/number/number.html rename to defaults/templates/bootstrap3/number/number.html diff --git a/templates/bootstrap3/number/range.html b/defaults/templates/bootstrap3/number/range.html similarity index 100% rename from templates/bootstrap3/number/range.html rename to defaults/templates/bootstrap3/number/range.html diff --git a/templates/bootstrap3/options/checkbox.html b/defaults/templates/bootstrap3/options/checkbox.html similarity index 100% rename from templates/bootstrap3/options/checkbox.html rename to defaults/templates/bootstrap3/options/checkbox.html diff --git a/templates/bootstrap3/options/radiobutton.html b/defaults/templates/bootstrap3/options/radiobutton.html similarity index 100% rename from templates/bootstrap3/options/radiobutton.html rename to defaults/templates/bootstrap3/options/radiobutton.html diff --git a/templates/bootstrap3/options/select.html b/defaults/templates/bootstrap3/options/select.html similarity index 100% rename from templates/bootstrap3/options/select.html rename to defaults/templates/bootstrap3/options/select.html diff --git a/templates/bootstrap3/static.html b/defaults/templates/bootstrap3/static.html similarity index 100% rename from templates/bootstrap3/static.html rename to defaults/templates/bootstrap3/static.html diff --git a/templates/bootstrap3/text/passwordinput.html b/defaults/templates/bootstrap3/text/passwordinput.html similarity index 100% rename from templates/bootstrap3/text/passwordinput.html rename to defaults/templates/bootstrap3/text/passwordinput.html diff --git a/templates/bootstrap3/text/textareainput.html b/defaults/templates/bootstrap3/text/textareainput.html similarity index 100% rename from templates/bootstrap3/text/textareainput.html rename to defaults/templates/bootstrap3/text/textareainput.html diff --git a/templates/bootstrap3/text/textinput.html b/defaults/templates/bootstrap3/text/textinput.html similarity index 100% rename from templates/bootstrap3/text/textinput.html rename to defaults/templates/bootstrap3/text/textinput.html diff --git a/templates/bootstrapform.html b/defaults/templates/bootstrapform.html similarity index 100% rename from templates/bootstrapform.html rename to defaults/templates/bootstrapform.html diff --git a/example/main.go b/example/main.go index 6755244..5cc3c32 100644 --- a/example/main.go +++ b/example/main.go @@ -6,6 +6,7 @@ import ( "time" . "github.com/coscms/forms" + _ "github.com/coscms/forms/defaults" ) type Test struct {