1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 23:30:54 +00:00
This commit is contained in:
astaxie 2013-09-12 13:44:12 +08:00
parent e788fb7239
commit f502f84423
2 changed files with 66 additions and 8 deletions

View File

@ -17,6 +17,7 @@ import (
"net/url" "net/url"
"os" "os"
"path" "path"
"regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -133,14 +134,52 @@ func (c *Controller) RenderBytes() ([]byte, error) {
panic("can't find templatefile in the path:" + c.TplNames) panic("can't find templatefile in the path:" + c.TplNames)
return []byte{}, errors.New("can't find templatefile in the path:" + c.TplNames) return []byte{}, errors.New("can't find templatefile in the path:" + c.TplNames)
} }
BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data) err := BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data)
if err != nil {
LayoutTplErrDeal:
if terr, ok := err.(*template.Error); ok {
if terr.ErrorCode == template.ErrNoSuchTemplate {
reg := regexp.MustCompile("\"(.+)\"")
a := reg.FindStringSubmatch(terr.Description)
if len(a) > 1 {
missfile := path.Join(ViewsPath, subdir, a[1])
AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
for k, v := range AllTemplateFiles.files {
BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...))
}
err = BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data)
if err != nil {
goto LayoutTplErrDeal
}
}
}
}
}
tplcontent, _ := ioutil.ReadAll(newbytes) tplcontent, _ := ioutil.ReadAll(newbytes)
c.Data["LayoutContent"] = template.HTML(string(tplcontent)) c.Data["LayoutContent"] = template.HTML(string(tplcontent))
subdir = path.Dir(c.Layout) subdir = path.Dir(c.Layout)
_, file = path.Split(c.Layout) _, file = path.Split(c.Layout)
ibytes := bytes.NewBufferString("") ibytes := bytes.NewBufferString("")
err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
if err != nil { if err != nil {
LayoutErrDeal:
if terr, ok := err.(*template.Error); ok {
if terr.ErrorCode == template.ErrNoSuchTemplate {
reg := regexp.MustCompile("\"(.+)\"")
a := reg.FindStringSubmatch(terr.Description)
if len(a) > 1 {
missfile := path.Join(ViewsPath, subdir, a[1])
AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
for k, v := range AllTemplateFiles.files {
BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...))
}
err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
if err != nil {
goto LayoutErrDeal
}
}
}
}
Trace("template Execute err:", err) Trace("template Execute err:", err)
} }
icontent, _ := ioutil.ReadAll(ibytes) icontent, _ := ioutil.ReadAll(ibytes)
@ -161,6 +200,24 @@ func (c *Controller) RenderBytes() ([]byte, error) {
} }
err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
if err != nil { if err != nil {
ErrDeal:
if terr, ok := err.(*template.Error); ok {
if terr.ErrorCode == template.ErrNoSuchTemplate {
reg := regexp.MustCompile("\"(.+)\"")
a := reg.FindStringSubmatch(terr.Description)
if len(a) > 1 {
missfile := path.Join(ViewsPath, subdir, a[1])
AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
for k, v := range AllTemplateFiles.files {
BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...))
}
err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
if err != nil {
goto ErrDeal
}
}
}
}
Trace("template Execute err:", err) Trace("template Execute err:", err)
} }
icontent, _ := ioutil.ReadAll(ibytes) icontent, _ := ioutil.ReadAll(ibytes)

View File

@ -13,9 +13,10 @@ import (
) )
var ( var (
beegoTplFuncMap template.FuncMap beegoTplFuncMap template.FuncMap
BeeTemplates map[string]*template.Template BeeTemplates map[string]*template.Template
BeeTemplateExt []string BeeTemplateExt []string
AllTemplateFiles *templatefile
) )
func init() { func init() {
@ -100,18 +101,18 @@ func BuildTemplate(dir string) error {
return errors.New("dir open err") return errors.New("dir open err")
} }
} }
self := templatefile{ AllTemplateFiles = &templatefile{
root: dir, root: dir,
files: make(map[string][]string), files: make(map[string][]string),
} }
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
return self.visit(path, f, err) return AllTemplateFiles.visit(path, f, err)
}) })
if err != nil { if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err) fmt.Printf("filepath.Walk() returned %v\n", err)
return err return err
} }
for k, v := range self.files { for k, v := range AllTemplateFiles.files {
BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...)) BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...))
} }
return nil return nil