diff --git a/template.go b/template.go index 88224830..011e488d 100644 --- a/template.go +++ b/template.go @@ -65,17 +65,11 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error { a = a[len([]byte(self.root)):] file := strings.TrimLeft(replace.Replace(string(a)), "/") subdir := filepath.Dir(file) - t, err := getTemplate(self.root, file) - if err != nil { - Trace("parse template err:", file, err) - } else { - BeeTemplates[file] = t - } if _, ok := self.files[subdir]; ok { - self.files[subdir] = append(self.files[subdir], paths) + self.files[subdir] = append(self.files[subdir], file) } else { m := make([]string, 1) - m[0] = paths + m[0] = file self.files[subdir] = m } @@ -119,6 +113,16 @@ func BuildTemplate(dir string) error { fmt.Printf("filepath.Walk() returned %v\n", err) return err } + for _, v := range self.files { + for _, file := range v { + t, err := getTemplate(self.root, file, v...) + if err != nil { + Trace("parse template err:", file, err) + } else { + BeeTemplates[file] = t + } + } + } return nil } @@ -152,9 +156,18 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, er return t, nil } -func getTemplate(root, file string) (t *template.Template, err error) { +func getTemplate(root, file string, others ...string) (t *template.Template, err error) { t = template.New(file).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap) t, err = getTplDeep(root, file, t) + for _, otherfile := range others { + if temp := t.Lookup(otherfile); temp != nil { + continue + } + t, err = getTplDeep(root, otherfile, t) + if err != nil { + break + } + } if err != nil { return nil, err } diff --git a/template_test.go b/template_test.go index 3d8c7a32..9e2f9c14 100644 --- a/template_test.go +++ b/template_test.go @@ -6,40 +6,64 @@ import ( "testing" ) -func TestBuildTemplate(t *testing.T) { +var header string = `{{define "header"}} +

Hello, astaxie!

+{{end}}` + +var index string = ` + + + beego welcome template + + +{{template "block"}} +{{template "header"}} +{{template "blocks/block.tpl"}} + + +` + +var block string = `{{define "block"}} +

Hello, blocks!

+{{end}}` + +func TestTemplate(t *testing.T) { dir := "_beeTmp" files := []string{ - "1.tpl", - "2.html", - "3.htmltpl", - "4.mystyle", + "header.tpl", + "index.tpl", + "blocks/block.tpl", } if err := os.MkdirAll(dir, 0777); err != nil { t.Fatal(err) } - for _, name := range files { + for k, name := range files { + os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777) if f, err := os.Create(filepath.Join(dir, name)); err != nil { t.Fatal(err) } else { + if k == 0 { + f.WriteString(header) + } else if k == 1 { + f.WriteString(index) + } else if k == 2 { + f.WriteString(block) + } + f.Close() } } if err := BuildTemplate(dir); err != nil { t.Fatal(err) } - if len(BeeTemplates) != 2 { - t.Fatalf("should be 2 but got %v", len(BeeTemplates)) - } - - AddTemplateExt("mystyle") - if err := BuildTemplate(dir); err != nil { - t.Fatal(err) - } if len(BeeTemplates) != 3 { t.Fatalf("should be 3 but got %v", len(BeeTemplates)) } - for _, name := range files { - os.Remove(filepath.Join(dir, name)) + if err := BeeTemplates["index.tpl"].ExecuteTemplate(os.Stdout, "index.tpl", nil); err != nil { + t.Fatal(err) } - os.Remove(dir) + for _, name := range files { + os.RemoveAll(filepath.Join(dir, name)) + } + os.RemoveAll(dir) }