mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 12:30:54 +00:00
Implemented possibility to add custom template engines
This commit is contained in:
parent
f6f34306ee
commit
10ddb06782
24
template.go
24
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
|
||||
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user