ffb347a2 by astaxie

support nest template

1 parent 19862725
...@@ -16,8 +16,6 @@ import ( ...@@ -16,8 +16,6 @@ import (
16 "net/http" 16 "net/http"
17 "net/url" 17 "net/url"
18 "os" 18 "os"
19 "path"
20 "regexp"
21 "strconv" 19 "strconv"
22 "strings" 20 "strings"
23 "time" 21 "time"
...@@ -127,74 +125,22 @@ func (c *Controller) RenderBytes() ([]byte, error) { ...@@ -127,74 +125,22 @@ func (c *Controller) RenderBytes() ([]byte, error) {
127 if RunMode == "dev" { 125 if RunMode == "dev" {
128 BuildTemplate(ViewsPath) 126 BuildTemplate(ViewsPath)
129 } 127 }
130 subdir := path.Dir(c.TplNames)
131 _, file := path.Split(c.TplNames)
132 newbytes := bytes.NewBufferString("") 128 newbytes := bytes.NewBufferString("")
133 if _, ok := BeeTemplates[subdir]; !ok { 129 if _, ok := BeeTemplates[c.TplNames]; !ok {
134 panic("can't find templatefile in the path:" + c.TplNames) 130 panic("can't find templatefile in the path:" + c.TplNames)
135 return []byte{}, errors.New("can't find templatefile in the path:" + c.TplNames) 131 return []byte{}, errors.New("can't find templatefile in the path:" + c.TplNames)
136 } 132 }
137 err := BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data) 133 err := BeeTemplates[c.TplNames].ExecuteTemplate(newbytes, c.TplNames, c.Data)
138 if err != nil { 134 if err != nil {
139 if terr, ok := err.(*template.Error); ok {
140 if terr.ErrorCode == template.ErrNoSuchTemplate {
141 reg := regexp.MustCompile("{{template \"(.+)\"")
142 filedata, _ := ioutil.ReadFile(path.Join(ViewsPath, c.TplNames))
143 a := reg.FindStringSubmatch(string(filedata))
144 if len(a) > 1 {
145 for _, tfile := range a[1:] {
146 missfile := path.Join(ViewsPath, subdir, tfile)
147 if ok, _ := FileExists(missfile); ok {
148 AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
149 }
150 }
151 for k, v := range AllTemplateFiles.files {
152 BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...))
153 }
154 err = BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data)
155 if err != nil {
156 Trace("template Execute err:", err)
157 }
158 goto LayoutTplOk
159 }
160 }
161 }
162 Trace("template Execute err:", err) 135 Trace("template Execute err:", err)
163 } 136 }
164 LayoutTplOk:
165 tplcontent, _ := ioutil.ReadAll(newbytes) 137 tplcontent, _ := ioutil.ReadAll(newbytes)
166 c.Data["LayoutContent"] = template.HTML(string(tplcontent)) 138 c.Data["LayoutContent"] = template.HTML(string(tplcontent))
167 subdir = path.Dir(c.Layout)
168 _, file = path.Split(c.Layout)
169 ibytes := bytes.NewBufferString("") 139 ibytes := bytes.NewBufferString("")
170 err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) 140 err = BeeTemplates[c.Layout].ExecuteTemplate(ibytes, c.Layout, c.Data)
171 if err != nil { 141 if err != nil {
172 if terr, ok := err.(*template.Error); ok {
173 if terr.ErrorCode == template.ErrNoSuchTemplate {
174 reg := regexp.MustCompile("{{template \"(.+)\"")
175 filedata, _ := ioutil.ReadFile(path.Join(ViewsPath, c.Layout))
176 a := reg.FindStringSubmatch(string(filedata))
177 if len(a) > 1 {
178 for _, tfile := range a[1:] {
179 missfile := path.Join(ViewsPath, subdir, tfile)
180 if ok, _ := FileExists(missfile); ok {
181 AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
182 }
183 }
184 for k, v := range AllTemplateFiles.files {
185 BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...))
186 }
187 err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
188 if err != nil {
189 Trace("template Execute err:", err)
190 }
191 goto LayoutOk
192 }
193 }
194 }
195 Trace("template Execute err:", err) 142 Trace("template Execute err:", err)
196 } 143 }
197 LayoutOk:
198 icontent, _ := ioutil.ReadAll(ibytes) 144 icontent, _ := ioutil.ReadAll(ibytes)
199 return icontent, nil 145 return icontent, nil
200 } else { 146 } else {
...@@ -204,40 +150,15 @@ func (c *Controller) RenderBytes() ([]byte, error) { ...@@ -204,40 +150,15 @@ func (c *Controller) RenderBytes() ([]byte, error) {
204 if RunMode == "dev" { 150 if RunMode == "dev" {
205 BuildTemplate(ViewsPath) 151 BuildTemplate(ViewsPath)
206 } 152 }
207 subdir := path.Dir(c.TplNames)
208 _, file := path.Split(c.TplNames)
209 ibytes := bytes.NewBufferString("") 153 ibytes := bytes.NewBufferString("")
210 if _, ok := BeeTemplates[subdir]; !ok { 154 if _, ok := BeeTemplates[c.TplNames]; !ok {
211 panic("can't find templatefile in the path:" + c.TplNames) 155 panic("can't find templatefile in the path:" + c.TplNames)
212 return []byte{}, errors.New("can't find templatefile in the path:" + c.TplNames) 156 return []byte{}, errors.New("can't find templatefile in the path:" + c.TplNames)
213 } 157 }
214 err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) 158 err := BeeTemplates[c.TplNames].ExecuteTemplate(ibytes, c.TplNames, c.Data)
215 if err != nil { 159 if err != nil {
216 if terr, ok := err.(*template.Error); ok { 160 Trace("template Execute err:", err)
217 if terr.ErrorCode == template.ErrNoSuchTemplate {
218 reg := regexp.MustCompile("{{template \"(.+)\"")
219 filedata, _ := ioutil.ReadFile(path.Join(ViewsPath, c.TplNames))
220 a := reg.FindStringSubmatch(string(filedata))
221 if len(a) > 1 {
222 for _, tfile := range a[1:] {
223 missfile := path.Join(ViewsPath, subdir, tfile)
224 if ok, _ := FileExists(missfile); ok {
225 AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
226 }
227 }
228 for k, v := range AllTemplateFiles.files {
229 BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...))
230 }
231 err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
232 if err != nil {
233 Trace("template Execute err:", err)
234 }
235 goto TplOk
236 }
237 }
238 }
239 } 161 }
240 TplOk:
241 icontent, _ := ioutil.ReadAll(ibytes) 162 icontent, _ := ioutil.ReadAll(ibytes)
242 return icontent, nil 163 return icontent, nil
243 } 164 }
......
...@@ -6,17 +6,17 @@ import ( ...@@ -6,17 +6,17 @@ import (
6 "errors" 6 "errors"
7 "fmt" 7 "fmt"
8 "html/template" 8 "html/template"
9 "io/ioutil"
9 "os" 10 "os"
10 "path"
11 "path/filepath" 11 "path/filepath"
12 "regexp"
12 "strings" 13 "strings"
13 ) 14 )
14 15
15 var ( 16 var (
16 beegoTplFuncMap template.FuncMap 17 beegoTplFuncMap template.FuncMap
17 BeeTemplates map[string]*template.Template 18 BeeTemplates map[string]*template.Template
18 BeeTemplateExt []string 19 BeeTemplateExt []string
19 AllTemplateFiles *templatefile
20 ) 20 )
21 21
22 func init() { 22 func init() {
...@@ -63,7 +63,14 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error { ...@@ -63,7 +63,14 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error {
63 replace := strings.NewReplacer("\\", "/") 63 replace := strings.NewReplacer("\\", "/")
64 a := []byte(paths) 64 a := []byte(paths)
65 a = a[len([]byte(self.root)):] 65 a = a[len([]byte(self.root)):]
66 subdir := path.Dir(strings.TrimLeft(replace.Replace(string(a)), "/")) 66 file := strings.TrimLeft(replace.Replace(string(a)), "/")
67 subdir := filepath.Dir(file)
68 t, err := getTemplate(file)
69 if err != nil {
70 Trace("parse template err:", file, err)
71 } else {
72 BeeTemplates[file] = t
73 }
67 if _, ok := self.files[subdir]; ok { 74 if _, ok := self.files[subdir]; ok {
68 self.files[subdir] = append(self.files[subdir], paths) 75 self.files[subdir] = append(self.files[subdir], paths)
69 } else { 76 } else {
...@@ -101,19 +108,55 @@ func BuildTemplate(dir string) error { ...@@ -101,19 +108,55 @@ func BuildTemplate(dir string) error {
101 return errors.New("dir open err") 108 return errors.New("dir open err")
102 } 109 }
103 } 110 }
104 AllTemplateFiles = &templatefile{ 111 self := &templatefile{
105 root: dir, 112 root: dir,
106 files: make(map[string][]string), 113 files: make(map[string][]string),
107 } 114 }
108 err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { 115 err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
109 return AllTemplateFiles.visit(path, f, err) 116 return self.visit(path, f, err)
110 }) 117 })
111 if err != nil { 118 if err != nil {
112 fmt.Printf("filepath.Walk() returned %v\n", err) 119 fmt.Printf("filepath.Walk() returned %v\n", err)
113 return err 120 return err
114 } 121 }
115 for k, v := range AllTemplateFiles.files {
116 BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...))
117 }
118 return nil 122 return nil
119 } 123 }
124
125 func getTplDeep(file string, t *template.Template) (*template.Template, error) {
126 fileabspath := filepath.Join(ViewsPath, file)
127 data, err := ioutil.ReadFile(fileabspath)
128 if err != nil {
129 return nil, err
130 }
131 t, err = t.New(file).Parse(string(data))
132 if err != nil {
133 return nil, err
134 }
135 reg := regexp.MustCompile("{{template \"(.+)\"")
136 allsub := reg.FindAllStringSubmatch(string(data), -1)
137 for _, m := range allsub {
138 if len(m) == 2 {
139 tlook := t.Lookup(m[1])
140 if tlook != nil {
141 continue
142 }
143 if !HasTemplateEXt(m[1]) {
144 continue
145 }
146 t, err = getTplDeep(m[1], t)
147 if err != nil {
148 return nil, err
149 }
150 }
151 }
152 return t, nil
153 }
154
155 func getTemplate(file string) (t *template.Template, err error) {
156 t = template.New(file).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap)
157 t, err = getTplDeep(file, t)
158 if err != nil {
159 return nil, err
160 }
161 return
162 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!