From 10ddb06782117ce34d1cadf48a285907f6933ef2 Mon Sep 17 00:00:00 2001 From: saturn4er Date: Mon, 7 Mar 2016 08:45:49 +0200 Subject: [PATCH 1/4] Implemented possibility to add custom template engines --- template.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/template.go b/template.go index e6c43f87..882e2c1f 100644 --- a/template.go +++ b/template.go @@ -32,10 +32,12 @@ import ( var ( beegoTplFuncMap = make(template.FuncMap) // beeTemplates caching map and supported template file extensions. - beeTemplates = make(map[string]*template.Template) + beeTemplates = make(map[string]TemplateI) templatesLock sync.RWMutex // beeTemplateExt stores the template extension which will build beeTemplateExt = []string{"tpl", "html"} + // BeeTemplatePreprocessors stores associations of extension -> preprocessor handler + BeeTemplateEngines = map[string]func(root, path string) (TemplateI, error){} ) func executeTemplate(wr io.Writer, name string, data interface{}) error { @@ -88,6 +90,9 @@ func AddFuncMap(key string, fn interface{}) error { return nil } +type TemplateI interface { + ExecuteTemplate(wr io.Writer, name string, data interface{}) error +} type templateFile struct { root string files map[string][]string @@ -101,7 +106,7 @@ func (tf *templateFile) visit(paths string, f os.FileInfo, err error) error { if f == nil { return err } - if f.IsDir() || (f.Mode()&os.ModeSymlink) > 0 { + if f.IsDir() || (f.Mode() & os.ModeSymlink) > 0 { return nil } if !HasTemplateExt(paths) { @@ -119,7 +124,7 @@ func (tf *templateFile) visit(paths string, f os.FileInfo, err error) error { // HasTemplateExt return this path contains supported template extension of beego or not. func HasTemplateExt(paths string) bool { for _, v := range beeTemplateExt { - if strings.HasSuffix(paths, "."+v) { + if strings.HasSuffix(paths, "." + v) { return true } } @@ -156,11 +161,18 @@ func BuildTemplate(dir string, files ...string) error { fmt.Printf("filepath.Walk() returned %v\n", err) return err } + buildAllFiles := len(files) == 0 for _, v := range self.files { for _, file := range v { - if len(files) == 0 || utils.InSlice(file, files) { + if buildAllFiles || utils.InSlice(file, files) { templatesLock.Lock() - t, err := getTemplate(self.root, file, v...) + fileExt := filepath.Ext(file)[1:] + var t TemplateI + if fn, ok := BeeTemplateEngines[fileExt]; ok { + t, err = fn(self.root, file) + }else { + t, err = getTemplate(self.root, file, v...) + } if err != nil { Trace("parse template err:", file, err) } else { @@ -305,3 +317,9 @@ func DelStaticPath(url string) *App { delete(BConfig.WebConfig.StaticDir, url) return BeeApp } + +func AddTemplateEngine(extension string, fn func(root, path string) (TemplateI, error)) *App { + AddTemplateExt(extension) + BeeTemplateEngines[extension] = fn + return BeeApp +} \ No newline at end of file From 9ee9f81861be6f2b0033756ee4d31ddd4d663df0 Mon Sep 17 00:00:00 2001 From: saturn4er Date: Mon, 7 Mar 2016 09:37:47 +0200 Subject: [PATCH 2/4] Add functions passing to template engine callback --- template.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/template.go b/template.go index 882e2c1f..b53e466e 100644 --- a/template.go +++ b/template.go @@ -31,13 +31,13 @@ import ( var ( beegoTplFuncMap = make(template.FuncMap) - // beeTemplates caching map and supported template file extensions. +// beeTemplates caching map and supported template file extensions. beeTemplates = make(map[string]TemplateI) templatesLock sync.RWMutex - // beeTemplateExt stores the template extension which will build +// beeTemplateExt stores the template extension which will build beeTemplateExt = []string{"tpl", "html"} - // BeeTemplatePreprocessors stores associations of extension -> preprocessor handler - BeeTemplateEngines = map[string]func(root, path string) (TemplateI, error){} +// BeeTemplatePreprocessors stores associations of extension -> preprocessor handler + BeeTemplateEngines = map[string]func(root, path string, funcs template.FuncMap) (TemplateI, error){} ) func executeTemplate(wr io.Writer, name string, data interface{}) error { @@ -169,7 +169,7 @@ func BuildTemplate(dir string, files ...string) error { fileExt := filepath.Ext(file)[1:] var t TemplateI if fn, ok := BeeTemplateEngines[fileExt]; ok { - t, err = fn(self.root, file) + t, err = fn(self.root, file, beegoTplFuncMap) }else { t, err = getTemplate(self.root, file, v...) } @@ -318,7 +318,7 @@ func DelStaticPath(url string) *App { return BeeApp } -func AddTemplateEngine(extension string, fn func(root, path string) (TemplateI, error)) *App { +func AddTemplateEngine(extension string, fn func(root, path string, funcs template.FuncMap) (TemplateI, error)) *App { AddTemplateExt(extension) BeeTemplateEngines[extension] = fn return BeeApp From 66423f693565b6b15fcbc6ad1708d418a370c0a2 Mon Sep 17 00:00:00 2001 From: saturn4er Date: Wed, 9 Mar 2016 10:40:12 +0200 Subject: [PATCH 3/4] Fix formatting with gofmt Rename BeeTemplateEngines->beeTemplateEngines. Create templateHandler function type Fix var name in comment BeeTemplatePreprocessors -> beeTemplatePreprocessors Rename TemplateI -> TemplateRenderer --- staticfile_test.go | 2 +- template.go | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/staticfile_test.go b/staticfile_test.go index e7003366..a043b4fd 100644 --- a/staticfile_test.go +++ b/staticfile_test.go @@ -7,8 +7,8 @@ import ( "io" "io/ioutil" "os" - "testing" "path/filepath" + "testing" ) var currentWorkDir, _ = os.Getwd() diff --git a/template.go b/template.go index b53e466e..f922e6ee 100644 --- a/template.go +++ b/template.go @@ -31,13 +31,13 @@ import ( var ( beegoTplFuncMap = make(template.FuncMap) -// beeTemplates caching map and supported template file extensions. - beeTemplates = make(map[string]TemplateI) + // beeTemplates caching map and supported template file extensions. + beeTemplates = make(map[string]TemplateRenderer) templatesLock sync.RWMutex -// beeTemplateExt stores the template extension which will build + // beeTemplateExt stores the template extension which will build beeTemplateExt = []string{"tpl", "html"} -// BeeTemplatePreprocessors stores associations of extension -> preprocessor handler - BeeTemplateEngines = map[string]func(root, path string, funcs template.FuncMap) (TemplateI, error){} + // beeTemplatePreprocessors stores associations of extension -> preprocessor handler + beeTemplateEngines = map[string]templateHandler{} ) func executeTemplate(wr io.Writer, name string, data interface{}) error { @@ -90,7 +90,8 @@ func AddFuncMap(key string, fn interface{}) error { return nil } -type TemplateI interface { +type templateHandler func(root, path string, funcs template.FuncMap) (TemplateRenderer, error) +type TemplateRenderer interface { ExecuteTemplate(wr io.Writer, name string, data interface{}) error } type templateFile struct { @@ -106,7 +107,7 @@ func (tf *templateFile) visit(paths string, f os.FileInfo, err error) error { if f == nil { return err } - if f.IsDir() || (f.Mode() & os.ModeSymlink) > 0 { + if f.IsDir() || (f.Mode()&os.ModeSymlink) > 0 { return nil } if !HasTemplateExt(paths) { @@ -124,7 +125,7 @@ func (tf *templateFile) visit(paths string, f os.FileInfo, err error) error { // HasTemplateExt return this path contains supported template extension of beego or not. func HasTemplateExt(paths string) bool { for _, v := range beeTemplateExt { - if strings.HasSuffix(paths, "." + v) { + if strings.HasSuffix(paths, "."+v) { return true } } @@ -167,10 +168,10 @@ func BuildTemplate(dir string, files ...string) error { if buildAllFiles || utils.InSlice(file, files) { templatesLock.Lock() fileExt := filepath.Ext(file)[1:] - var t TemplateI - if fn, ok := BeeTemplateEngines[fileExt]; ok { + var t TemplateRenderer + if fn, ok := beeTemplateEngines[fileExt]; ok { t, err = fn(self.root, file, beegoTplFuncMap) - }else { + } else { t, err = getTemplate(self.root, file, v...) } if err != nil { @@ -318,8 +319,8 @@ func DelStaticPath(url string) *App { return BeeApp } -func AddTemplateEngine(extension string, fn func(root, path string, funcs template.FuncMap) (TemplateI, error)) *App { +func AddTemplateEngine(extension string, fn templateHandler) *App { AddTemplateExt(extension) - BeeTemplateEngines[extension] = fn + beeTemplateEngines[extension] = fn return BeeApp -} \ No newline at end of file +} From adaa4ab929cd3cdca95010a7a77885cf373e7155 Mon Sep 17 00:00:00 2001 From: saturn4er Date: Tue, 29 Mar 2016 17:15:43 +0300 Subject: [PATCH 4/4] Fix index out of range if there is no file extension --- template.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/template.go b/template.go index f922e6ee..5cc627a5 100644 --- a/template.go +++ b/template.go @@ -167,9 +167,11 @@ func BuildTemplate(dir string, files ...string) error { for _, file := range v { if buildAllFiles || utils.InSlice(file, files) { templatesLock.Lock() - fileExt := filepath.Ext(file)[1:] + ext := filepath.Ext(file) var t TemplateRenderer - if fn, ok := beeTemplateEngines[fileExt]; ok { + if len(ext) == 0 { + t, err = getTemplate(self.root, file, v...) + } else if fn, ok := beeTemplateEngines[ext[1:]]; ok { t, err = fn(self.root, file, beegoTplFuncMap) } else { t, err = getTemplate(self.root, file, v...)