Commit 7b051e7a authored by astaxie's avatar astaxie

Merge pull request #1940 from MachineShop-IOT/develop

More flexible support for template engines
parents cef91db2 24b88706
...@@ -33,12 +33,12 @@ import ( ...@@ -33,12 +33,12 @@ import (
var ( var (
beegoTplFuncMap = make(template.FuncMap) beegoTplFuncMap = make(template.FuncMap)
// beeTemplates caching map and supported template file extensions. // beeTemplates caching map and supported template file extensions.
beeTemplates = make(map[string]TemplateRenderer) beeTemplates = make(map[string]*template.Template)
templatesLock sync.RWMutex templatesLock sync.RWMutex
// beeTemplateExt stores the template extension which will build // beeTemplateExt stores the template extension which will build
beeTemplateExt = []string{"tpl", "html"} beeTemplateExt = []string{"tpl", "html"}
// beeTemplatePreprocessors stores associations of extension -> preprocessor handler // beeTemplatePreprocessors stores associations of extension -> preprocessor handler
beeTemplateEngines = map[string]templateHandler{} beeTemplateEngines = map[string]templatePreProcessor{}
) )
// ExecuteTemplate applies the template with name to the specified data object, // ExecuteTemplate applies the template with name to the specified data object,
...@@ -50,11 +50,22 @@ func ExecuteTemplate(wr io.Writer, name string, data interface{}) error { ...@@ -50,11 +50,22 @@ func ExecuteTemplate(wr io.Writer, name string, data interface{}) error {
defer templatesLock.RUnlock() defer templatesLock.RUnlock()
} }
if t, ok := beeTemplates[name]; ok { if t, ok := beeTemplates[name]; ok {
if t.Lookup(name) != nil {
err := t.ExecuteTemplate(wr, name, data) err := t.ExecuteTemplate(wr, name, data)
if err != nil { if err != nil {
logs.Trace("template Execute err:", err) logs.Trace("template Execute err:", err)
} }
return err return err
} else {
err := t.Execute(wr, data)
if err != nil {
if err != nil {
logs.Trace("template Execute err:", err)
}
return err
}
}
return nil
} }
panic("can't find templatefile in the path:" + name) panic("can't find templatefile in the path:" + name)
} }
...@@ -94,10 +105,8 @@ func AddFuncMap(key string, fn interface{}) error { ...@@ -94,10 +105,8 @@ func AddFuncMap(key string, fn interface{}) error {
return nil return nil
} }
type templateHandler func(root, path string, funcs template.FuncMap) (TemplateRenderer, error) type templatePreProcessor func(root, path string, funcs template.FuncMap) (*template.Template, error)
type TemplateRenderer interface {
ExecuteTemplate(wr io.Writer, name string, data interface{}) error
}
type templateFile struct { type templateFile struct {
root string root string
files map[string][]string files map[string][]string
...@@ -172,7 +181,7 @@ func BuildTemplate(dir string, files ...string) error { ...@@ -172,7 +181,7 @@ func BuildTemplate(dir string, files ...string) error {
if buildAllFiles || utils.InSlice(file, files) { if buildAllFiles || utils.InSlice(file, files) {
templatesLock.Lock() templatesLock.Lock()
ext := filepath.Ext(file) ext := filepath.Ext(file)
var t TemplateRenderer var t *template.Template
if len(ext) == 0 { if len(ext) == 0 {
t, err = getTemplate(self.root, file, v...) t, err = getTemplate(self.root, file, v...)
} else if fn, ok := beeTemplateEngines[ext[1:]]; ok { } else if fn, ok := beeTemplateEngines[ext[1:]]; ok {
...@@ -325,7 +334,7 @@ func DelStaticPath(url string) *App { ...@@ -325,7 +334,7 @@ func DelStaticPath(url string) *App {
return BeeApp return BeeApp
} }
func AddTemplateEngine(extension string, fn templateHandler) *App { func AddTemplateEngine(extension string, fn templatePreProcessor) *App {
AddTemplateExt(extension) AddTemplateExt(extension)
beeTemplateEngines[extension] = fn beeTemplateEngines[extension] = fn
return BeeApp return BeeApp
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment