0476da50 by astaxie

improve template

1 parent 943fe971
...@@ -126,15 +126,15 @@ func BuildTemplate(dir string) error { ...@@ -126,15 +126,15 @@ func BuildTemplate(dir string) error {
126 return nil 126 return nil
127 } 127 }
128 128
129 func getTplDeep(root, file string, t *template.Template) (*template.Template, error) { 129 func getTplDeep(root, file string, t *template.Template) (*template.Template, [][]string, error) {
130 fileabspath := filepath.Join(root, file) 130 fileabspath := filepath.Join(root, file)
131 data, err := ioutil.ReadFile(fileabspath) 131 data, err := ioutil.ReadFile(fileabspath)
132 if err != nil { 132 if err != nil {
133 return nil, err 133 return nil, [][]string{}, err
134 } 134 }
135 t, err = t.New(file).Parse(string(data)) 135 t, err = t.New(file).Parse(string(data))
136 if err != nil { 136 if err != nil {
137 return nil, err 137 return nil, [][]string{}, err
138 } 138 }
139 reg := regexp.MustCompile("{{[ ]*template[ ]+\"([^\"]+)\"") 139 reg := regexp.MustCompile("{{[ ]*template[ ]+\"([^\"]+)\"")
140 allsub := reg.FindAllStringSubmatch(string(data), -1) 140 allsub := reg.FindAllStringSubmatch(string(data), -1)
...@@ -147,27 +147,58 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, er ...@@ -147,27 +147,58 @@ func getTplDeep(root, file string, t *template.Template) (*template.Template, er
147 if !HasTemplateEXt(m[1]) { 147 if !HasTemplateEXt(m[1]) {
148 continue 148 continue
149 } 149 }
150 t, err = getTplDeep(root, m[1], t) 150 t, _, err = getTplDeep(root, m[1], t)
151 if err != nil { 151 if err != nil {
152 return nil, err 152 return nil, [][]string{}, err
153 } 153 }
154 } 154 }
155 } 155 }
156 return t, nil 156 return t, allsub, nil
157 } 157 }
158 158
159 func getTemplate(root, file string, others ...string) (t *template.Template, err error) { 159 func getTemplate(root, file string, others ...string) (t *template.Template, err error) {
160 t = template.New(file).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap) 160 t = template.New(file).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap)
161 t, err = getTplDeep(root, file, t) 161 var submods [][]string
162 t, submods, err = getTplDeep(root, file, t)
163 for _, m := range submods {
164 if len(m) == 2 {
165 templ := t.Lookup(m[1])
166 if templ != nil {
167 continue
168 }
169 //first check filename
162 for _, otherfile := range others { 170 for _, otherfile := range others {
163 if temp := t.Lookup(otherfile); temp != nil { 171 if otherfile == m[1] {
172 t, _, err = getTplDeep(root, otherfile, t)
173 if err != nil {
174 Trace("template parse file err:", err)
175 }
176 break
177 }
178 }
179 //second check define
180 for _, otherfile := range others {
181 fileabspath := filepath.Join(root, otherfile)
182 data, err := ioutil.ReadFile(fileabspath)
183 if err != nil {
164 continue 184 continue
165 } 185 }
166 t, err = getTplDeep(root, otherfile, t) 186 reg := regexp.MustCompile("{{[ ]*define[ ]+\"([^\"]+)\"")
187 allsub := reg.FindAllStringSubmatch(string(data), -1)
188 for _, sub := range allsub {
189 if len(sub) == 2 && sub[1] == m[1] {
190 t, _, err = getTplDeep(root, otherfile, t)
167 if err != nil { 191 if err != nil {
192 Trace("template parse file err:", err)
193 }
168 break 194 break
169 } 195 }
170 } 196 }
197 }
198 }
199
200 }
201
171 if err != nil { 202 if err != nil {
172 return nil, err 203 return nil, err
173 } 204 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!