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

improve template

This commit is contained in:
astaxie 2013-09-13 17:41:31 +08:00
parent 943fe971f1
commit 0476da503e

View File

@ -126,15 +126,15 @@ func BuildTemplate(dir string) error {
return nil return nil
} }
func getTplDeep(root, file string, t *template.Template) (*template.Template, error) { func getTplDeep(root, file string, t *template.Template) (*template.Template, [][]string, error) {
fileabspath := filepath.Join(root, file) fileabspath := filepath.Join(root, file)
data, err := ioutil.ReadFile(fileabspath) data, err := ioutil.ReadFile(fileabspath)
if err != nil { if err != nil {
return nil, err return nil, [][]string{}, err
} }
t, err = t.New(file).Parse(string(data)) t, err = t.New(file).Parse(string(data))
if err != nil { if err != nil {
return nil, err return nil, [][]string{}, err
} }
reg := regexp.MustCompile("{{[ ]*template[ ]+\"([^\"]+)\"") reg := regexp.MustCompile("{{[ ]*template[ ]+\"([^\"]+)\"")
allsub := reg.FindAllStringSubmatch(string(data), -1) allsub := reg.FindAllStringSubmatch(string(data), -1)
@ -147,27 +147,58 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, er
if !HasTemplateEXt(m[1]) { if !HasTemplateEXt(m[1]) {
continue continue
} }
t, err = getTplDeep(root, m[1], t) t, _, err = getTplDeep(root, m[1], t)
if err != nil { if err != nil {
return nil, err return nil, [][]string{}, err
} }
} }
} }
return t, nil return t, allsub, nil
} }
func getTemplate(root, file string, others ...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) var submods [][]string
for _, otherfile := range others { t, submods, err = getTplDeep(root, file, t)
if temp := t.Lookup(otherfile); temp != nil { for _, m := range submods {
if len(m) == 2 {
templ := t.Lookup(m[1])
if templ != nil {
continue continue
} }
t, err = getTplDeep(root, otherfile, t) //first check filename
for _, otherfile := range others {
if otherfile == m[1] {
t, _, err = getTplDeep(root, otherfile, t)
if err != nil { if err != nil {
Trace("template parse file err:", err)
}
break break
} }
} }
//second check define
for _, otherfile := range others {
fileabspath := filepath.Join(root, otherfile)
data, err := ioutil.ReadFile(fileabspath)
if err != nil {
continue
}
reg := regexp.MustCompile("{{[ ]*define[ ]+\"([^\"]+)\"")
allsub := reg.FindAllStringSubmatch(string(data), -1)
for _, sub := range allsub {
if len(sub) == 2 && sub[1] == m[1] {
t, _, err = getTplDeep(root, otherfile, t)
if err != nil {
Trace("template parse file err:", err)
}
break
}
}
}
}
}
if err != nil { if err != nil {
return nil, err return nil, err
} }