1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 17:00:54 +00:00

Implemented possibility to add custom template engines

This commit is contained in:
saturn4er 2016-03-07 08:45:49 +02:00
parent f6f34306ee
commit 10ddb06782

View File

@ -32,10 +32,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]*template.Template) beeTemplates = make(map[string]TemplateI)
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
BeeTemplateEngines = map[string]func(root, path string) (TemplateI, error){}
) )
func executeTemplate(wr io.Writer, name string, data interface{}) error { func executeTemplate(wr io.Writer, name string, data interface{}) error {
@ -88,6 +90,9 @@ func AddFuncMap(key string, fn interface{}) error {
return nil return nil
} }
type TemplateI 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
@ -101,7 +106,7 @@ func (tf *templateFile) visit(paths string, f os.FileInfo, err error) error {
if f == nil { if f == nil {
return err return err
} }
if f.IsDir() || (f.Mode()&os.ModeSymlink) > 0 { if f.IsDir() || (f.Mode() & os.ModeSymlink) > 0 {
return nil return nil
} }
if !HasTemplateExt(paths) { 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. // HasTemplateExt return this path contains supported template extension of beego or not.
func HasTemplateExt(paths string) bool { func HasTemplateExt(paths string) bool {
for _, v := range beeTemplateExt { for _, v := range beeTemplateExt {
if strings.HasSuffix(paths, "."+v) { if strings.HasSuffix(paths, "." + v) {
return true return true
} }
} }
@ -156,11 +161,18 @@ func BuildTemplate(dir string, files ...string) error {
fmt.Printf("filepath.Walk() returned %v\n", err) fmt.Printf("filepath.Walk() returned %v\n", err)
return err return err
} }
buildAllFiles := len(files) == 0
for _, v := range self.files { for _, v := range self.files {
for _, file := range v { for _, file := range v {
if len(files) == 0 || utils.InSlice(file, files) { if buildAllFiles || utils.InSlice(file, files) {
templatesLock.Lock() 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 { if err != nil {
Trace("parse template err:", file, err) Trace("parse template err:", file, err)
} else { } else {
@ -305,3 +317,9 @@ func DelStaticPath(url string) *App {
delete(BConfig.WebConfig.StaticDir, url) delete(BConfig.WebConfig.StaticDir, url)
return BeeApp return BeeApp
} }
func AddTemplateEngine(extension string, fn func(root, path string) (TemplateI, error)) *App {
AddTemplateExt(extension)
BeeTemplateEngines[extension] = fn
return BeeApp
}