1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 19:30:51 +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)):]
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
}

View File

@ -6,40 +6,64 @@ import (
"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"
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)
}