diff --git a/controller.go b/controller.go index 3e85d813..9c2df8e3 100644 --- a/controller.go +++ b/controller.go @@ -17,6 +17,7 @@ import ( "net/url" "os" "path" + "regexp" "strconv" "strings" "time" @@ -133,14 +134,52 @@ func (c *Controller) RenderBytes() ([]byte, error) { panic("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) c.Data["LayoutContent"] = template.HTML(string(tplcontent)) subdir = path.Dir(c.Layout) _, file = path.Split(c.Layout) ibytes := bytes.NewBufferString("") - err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) + err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) 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) } icontent, _ := ioutil.ReadAll(ibytes) @@ -161,6 +200,24 @@ func (c *Controller) RenderBytes() ([]byte, error) { } err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) 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) } icontent, _ := ioutil.ReadAll(ibytes) diff --git a/template.go b/template.go index 1fae0d37..ffcdc695 100644 --- a/template.go +++ b/template.go @@ -13,9 +13,10 @@ import ( ) var ( - beegoTplFuncMap template.FuncMap - BeeTemplates map[string]*template.Template - BeeTemplateExt []string + beegoTplFuncMap template.FuncMap + BeeTemplates map[string]*template.Template + BeeTemplateExt []string + AllTemplateFiles *templatefile ) func init() { @@ -100,18 +101,18 @@ func BuildTemplate(dir string) error { return errors.New("dir open err") } } - self := templatefile{ + AllTemplateFiles = &templatefile{ root: dir, files: make(map[string][]string), } 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 { fmt.Printf("filepath.Walk() returned %v\n", 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...)) } return nil