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

support template

This commit is contained in:
astaxie 2013-09-13 13:57:40 +08:00
parent 9d84969bf6
commit 943fe971f1
2 changed files with 63 additions and 26 deletions

View File

@ -65,17 +65,11 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error {
a = a[len([]byte(self.root)):] a = a[len([]byte(self.root)):]
file := strings.TrimLeft(replace.Replace(string(a)), "/") file := strings.TrimLeft(replace.Replace(string(a)), "/")
subdir := filepath.Dir(file) 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 { if _, ok := self.files[subdir]; ok {
self.files[subdir] = append(self.files[subdir], paths) self.files[subdir] = append(self.files[subdir], file)
} else { } else {
m := make([]string, 1) m := make([]string, 1)
m[0] = paths m[0] = file
self.files[subdir] = m self.files[subdir] = m
} }
@ -119,6 +113,16 @@ func BuildTemplate(dir string) error {
fmt.Printf("filepath.Walk() returned %v\n", err) fmt.Printf("filepath.Walk() returned %v\n", err)
return 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 return nil
} }
@ -152,9 +156,18 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, er
return t, nil 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 = template.New(file).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap)
t, err = getTplDeep(root, file, t) 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -6,40 +6,64 @@ import (
"testing" "testing"
) )
func TestBuildTemplate(t *testing.T) { var header string = `{{define "header"}}
<h1>Hello, astaxie!</h1>
{{end}}`
var index string = `<!DOCTYPE html>
<html>
<head>
<title>beego welcome template</title>
</head>
<body>
{{template "block"}}
{{template "header"}}
{{template "blocks/block.tpl"}}
</body>
</html>
`
var block string = `{{define "block"}}
<h1>Hello, blocks!</h1>
{{end}}`
func TestTemplate(t *testing.T) {
dir := "_beeTmp" dir := "_beeTmp"
files := []string{ files := []string{
"1.tpl", "header.tpl",
"2.html", "index.tpl",
"3.htmltpl", "blocks/block.tpl",
"4.mystyle",
} }
if err := os.MkdirAll(dir, 0777); err != nil { if err := os.MkdirAll(dir, 0777); err != nil {
t.Fatal(err) 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 { if f, err := os.Create(filepath.Join(dir, name)); err != nil {
t.Fatal(err) t.Fatal(err)
} else { } else {
if k == 0 {
f.WriteString(header)
} else if k == 1 {
f.WriteString(index)
} else if k == 2 {
f.WriteString(block)
}
f.Close() f.Close()
} }
} }
if err := BuildTemplate(dir); err != nil { if err := BuildTemplate(dir); err != nil {
t.Fatal(err) 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 { if len(BeeTemplates) != 3 {
t.Fatalf("should be 3 but got %v", len(BeeTemplates)) t.Fatalf("should be 3 but got %v", len(BeeTemplates))
} }
for _, name := range files { if err := BeeTemplates["index.tpl"].ExecuteTemplate(os.Stdout, "index.tpl", nil); err != nil {
os.Remove(filepath.Join(dir, name)) t.Fatal(err)
} }
os.Remove(dir) for _, name := range files {
os.RemoveAll(filepath.Join(dir, name))
}
os.RemoveAll(dir)
} }