From 0476da503e95cef9e829fa26e7399c0174c66b95 Mon Sep 17 00:00:00 2001 From: astaxie Date: Fri, 13 Sep 2013 17:41:31 +0800 Subject: [PATCH] improve template --- template.go | 59 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/template.go b/template.go index 011e488d..f859c2b8 100644 --- a/template.go +++ b/template.go @@ -126,15 +126,15 @@ func BuildTemplate(dir string) error { 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) data, err := ioutil.ReadFile(fileabspath) if err != nil { - return nil, err + return nil, [][]string{}, err } t, err = t.New(file).Parse(string(data)) if err != nil { - return nil, err + return nil, [][]string{}, err } reg := regexp.MustCompile("{{[ ]*template[ ]+\"([^\"]+)\"") 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]) { continue } - t, err = getTplDeep(root, m[1], t) + t, _, err = getTplDeep(root, m[1], t) 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) { 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 + var submods [][]string + t, submods, err = getTplDeep(root, file, t) + for _, m := range submods { + if len(m) == 2 { + templ := t.Lookup(m[1]) + if templ != nil { + continue + } + //first check filename + for _, otherfile := range others { + if otherfile == m[1] { + t, _, err = getTplDeep(root, otherfile, t) + if err != nil { + Trace("template parse file err:", err) + } + 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 { return nil, err }