support template
Showing
2 changed files
with
60 additions
and
30 deletions
| ... | @@ -136,25 +136,32 @@ func (c *Controller) RenderBytes() ([]byte, error) { | ... | @@ -136,25 +136,32 @@ func (c *Controller) RenderBytes() ([]byte, error) { |
| 136 | } | 136 | } |
| 137 | err := BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data) | 137 | err := BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data) |
| 138 | if err != nil { | 138 | if err != nil { |
| 139 | LayoutTplErrDeal: | ||
| 140 | if terr, ok := err.(*template.Error); ok { | 139 | if terr, ok := err.(*template.Error); ok { |
| 141 | if terr.ErrorCode == template.ErrNoSuchTemplate { | 140 | if terr.ErrorCode == template.ErrNoSuchTemplate { |
| 142 | reg := regexp.MustCompile("\"(.+)\"") | 141 | reg := regexp.MustCompile("{{template \"(.+)\"") |
| 143 | a := reg.FindStringSubmatch(terr.Description) | 142 | filedata, _ := ioutil.ReadFile(path.Join(ViewsPath, c.TplNames)) |
| 143 | a := reg.FindStringSubmatch(string(filedata)) | ||
| 144 | if len(a) > 1 { | 144 | if len(a) > 1 { |
| 145 | missfile := path.Join(ViewsPath, subdir, a[1]) | 145 | for _, tfile := range a[1:] { |
| 146 | AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile) | 146 | missfile := path.Join(ViewsPath, subdir, tfile) |
| 147 | if ok, _ := FileExists(missfile); ok { | ||
| 148 | AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile) | ||
| 149 | } | ||
| 150 | } | ||
| 147 | for k, v := range AllTemplateFiles.files { | 151 | for k, v := range AllTemplateFiles.files { |
| 148 | BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...)) | 152 | BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...)) |
| 149 | } | 153 | } |
| 150 | err = BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data) | 154 | err = BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data) |
| 151 | if err != nil { | 155 | if err != nil { |
| 152 | goto LayoutTplErrDeal | 156 | Trace("template Execute err:", err) |
| 153 | } | 157 | } |
| 158 | goto LayoutTplOk | ||
| 154 | } | 159 | } |
| 155 | } | 160 | } |
| 156 | } | 161 | } |
| 162 | Trace("template Execute err:", err) | ||
| 157 | } | 163 | } |
| 164 | LayoutTplOk: | ||
| 158 | tplcontent, _ := ioutil.ReadAll(newbytes) | 165 | tplcontent, _ := ioutil.ReadAll(newbytes) |
| 159 | c.Data["LayoutContent"] = template.HTML(string(tplcontent)) | 166 | c.Data["LayoutContent"] = template.HTML(string(tplcontent)) |
| 160 | subdir = path.Dir(c.Layout) | 167 | subdir = path.Dir(c.Layout) |
| ... | @@ -162,26 +169,32 @@ func (c *Controller) RenderBytes() ([]byte, error) { | ... | @@ -162,26 +169,32 @@ func (c *Controller) RenderBytes() ([]byte, error) { |
| 162 | ibytes := bytes.NewBufferString("") | 169 | ibytes := bytes.NewBufferString("") |
| 163 | err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) | 170 | err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) |
| 164 | if err != nil { | 171 | if err != nil { |
| 165 | LayoutErrDeal: | ||
| 166 | if terr, ok := err.(*template.Error); ok { | 172 | if terr, ok := err.(*template.Error); ok { |
| 167 | if terr.ErrorCode == template.ErrNoSuchTemplate { | 173 | if terr.ErrorCode == template.ErrNoSuchTemplate { |
| 168 | reg := regexp.MustCompile("\"(.+)\"") | 174 | reg := regexp.MustCompile("{{template \"(.+)\"") |
| 169 | a := reg.FindStringSubmatch(terr.Description) | 175 | filedata, _ := ioutil.ReadFile(path.Join(ViewsPath, c.Layout)) |
| 176 | a := reg.FindStringSubmatch(string(filedata)) | ||
| 170 | if len(a) > 1 { | 177 | if len(a) > 1 { |
| 171 | missfile := path.Join(ViewsPath, subdir, a[1]) | 178 | for _, tfile := range a[1:] { |
| 172 | AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile) | 179 | missfile := path.Join(ViewsPath, subdir, tfile) |
| 180 | if ok, _ := FileExists(missfile); ok { | ||
| 181 | AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile) | ||
| 182 | } | ||
| 183 | } | ||
| 173 | for k, v := range AllTemplateFiles.files { | 184 | for k, v := range AllTemplateFiles.files { |
| 174 | BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...)) | 185 | BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...)) |
| 175 | } | 186 | } |
| 176 | err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) | 187 | err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) |
| 177 | if err != nil { | 188 | if err != nil { |
| 178 | goto LayoutErrDeal | 189 | Trace("template Execute err:", err) |
| 179 | } | 190 | } |
| 191 | goto LayoutOk | ||
| 180 | } | 192 | } |
| 181 | } | 193 | } |
| 182 | } | 194 | } |
| 183 | Trace("template Execute err:", err) | 195 | Trace("template Execute err:", err) |
| 184 | } | 196 | } |
| 197 | LayoutOk: | ||
| 185 | icontent, _ := ioutil.ReadAll(ibytes) | 198 | icontent, _ := ioutil.ReadAll(ibytes) |
| 186 | return icontent, nil | 199 | return icontent, nil |
| 187 | } else { | 200 | } else { |
| ... | @@ -200,26 +213,31 @@ func (c *Controller) RenderBytes() ([]byte, error) { | ... | @@ -200,26 +213,31 @@ func (c *Controller) RenderBytes() ([]byte, error) { |
| 200 | } | 213 | } |
| 201 | err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) | 214 | err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) |
| 202 | if err != nil { | 215 | if err != nil { |
| 203 | ErrDeal: | ||
| 204 | if terr, ok := err.(*template.Error); ok { | 216 | if terr, ok := err.(*template.Error); ok { |
| 205 | if terr.ErrorCode == template.ErrNoSuchTemplate { | 217 | if terr.ErrorCode == template.ErrNoSuchTemplate { |
| 206 | reg := regexp.MustCompile("\"(.+)\"") | 218 | reg := regexp.MustCompile("{{template \"(.+)\"") |
| 207 | a := reg.FindStringSubmatch(terr.Description) | 219 | filedata, _ := ioutil.ReadFile(path.Join(ViewsPath, c.TplNames)) |
| 220 | a := reg.FindStringSubmatch(string(filedata)) | ||
| 208 | if len(a) > 1 { | 221 | if len(a) > 1 { |
| 209 | missfile := path.Join(ViewsPath, subdir, a[1]) | 222 | for _, tfile := range a[1:] { |
| 210 | AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile) | 223 | missfile := path.Join(ViewsPath, subdir, tfile) |
| 224 | if ok, _ := FileExists(missfile); ok { | ||
| 225 | AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile) | ||
| 226 | } | ||
| 227 | } | ||
| 211 | for k, v := range AllTemplateFiles.files { | 228 | for k, v := range AllTemplateFiles.files { |
| 212 | BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...)) | 229 | BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...)) |
| 213 | } | 230 | } |
| 214 | err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) | 231 | err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data) |
| 215 | if err != nil { | 232 | if err != nil { |
| 216 | goto ErrDeal | 233 | Trace("template Execute err:", err) |
| 217 | } | 234 | } |
| 235 | goto TplOk | ||
| 218 | } | 236 | } |
| 219 | } | 237 | } |
| 220 | } | 238 | } |
| 221 | Trace("template Execute err:", err) | ||
| 222 | } | 239 | } |
| 240 | TplOk: | ||
| 223 | icontent, _ := ioutil.ReadAll(ibytes) | 241 | icontent, _ := ioutil.ReadAll(ibytes) |
| 224 | return icontent, nil | 242 | return icontent, nil |
| 225 | } | 243 | } | ... | ... |
| ... | @@ -4,6 +4,7 @@ import ( | ... | @@ -4,6 +4,7 @@ import ( |
| 4 | "fmt" | 4 | "fmt" |
| 5 | "html/template" | 5 | "html/template" |
| 6 | "net/url" | 6 | "net/url" |
| 7 | "os" | ||
| 7 | "reflect" | 8 | "reflect" |
| 8 | "regexp" | 9 | "regexp" |
| 9 | "strconv" | 10 | "strconv" |
| ... | @@ -68,14 +69,14 @@ func DateFormat(t time.Time, layout string) (datestring string) { | ... | @@ -68,14 +69,14 @@ func DateFormat(t time.Time, layout string) (datestring string) { |
| 68 | 69 | ||
| 69 | var DatePatterns = []string{ | 70 | var DatePatterns = []string{ |
| 70 | // year | 71 | // year |
| 71 | "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 | 72 | "Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 |
| 72 | "y", "06", //A two digit representation of a year Examples: 99 or 03 | 73 | "y", "06", //A two digit representation of a year Examples: 99 or 03 |
| 73 | 74 | ||
| 74 | // month | 75 | // month |
| 75 | "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 | 76 | "m", "01", // Numeric representation of a month, with leading zeros 01 through 12 |
| 76 | "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 | 77 | "n", "1", // Numeric representation of a month, without leading zeros 1 through 12 |
| 77 | "M", "Jan", // A short textual representation of a month, three letters Jan through Dec | 78 | "M", "Jan", // A short textual representation of a month, three letters Jan through Dec |
| 78 | "F", "January", // A full textual representation of a month, such as January or March January through December | 79 | "F", "January", // A full textual representation of a month, such as January or March January through December |
| 79 | 80 | ||
| 80 | // day | 81 | // day |
| 81 | "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 | 82 | "d", "02", // Day of the month, 2 digits with leading zeros 01 to 31 |
| ... | @@ -83,19 +84,19 @@ var DatePatterns = []string{ | ... | @@ -83,19 +84,19 @@ var DatePatterns = []string{ |
| 83 | 84 | ||
| 84 | // week | 85 | // week |
| 85 | "D", "Mon", // A textual representation of a day, three letters Mon through Sun | 86 | "D", "Mon", // A textual representation of a day, three letters Mon through Sun |
| 86 | "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday | 87 | "l", "Monday", // A full textual representation of the day of the week Sunday through Saturday |
| 87 | 88 | ||
| 88 | // time | 89 | // time |
| 89 | "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 | 90 | "g", "3", // 12-hour format of an hour without leading zeros 1 through 12 |
| 90 | "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 | 91 | "G", "15", // 24-hour format of an hour without leading zeros 0 through 23 |
| 91 | "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 | 92 | "h", "03", // 12-hour format of an hour with leading zeros 01 through 12 |
| 92 | "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 | 93 | "H", "15", // 24-hour format of an hour with leading zeros 00 through 23 |
| 93 | 94 | ||
| 94 | "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm | 95 | "a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm |
| 95 | "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM | 96 | "A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM |
| 96 | 97 | ||
| 97 | "i", "04", // Minutes with leading zeros 00 to 59 | 98 | "i", "04", // Minutes with leading zeros 00 to 59 |
| 98 | "s", "05", // Seconds, with leading zeros 00 through 59 | 99 | "s", "05", // Seconds, with leading zeros 00 through 59 |
| 99 | 100 | ||
| 100 | // time zone | 101 | // time zone |
| 101 | "T", "MST", | 102 | "T", "MST", |
| ... | @@ -350,3 +351,14 @@ func stringsToJson(str string) string { | ... | @@ -350,3 +351,14 @@ func stringsToJson(str string) string { |
| 350 | } | 351 | } |
| 351 | return jsons | 352 | return jsons |
| 352 | } | 353 | } |
| 354 | |||
| 355 | func FileExists(path string) (bool, error) { | ||
| 356 | _, err := os.Stat(path) | ||
| 357 | if err == nil { | ||
| 358 | return true, nil | ||
| 359 | } | ||
| 360 | if os.IsNotExist(err) { | ||
| 361 | return false, nil | ||
| 362 | } | ||
| 363 | return false, err | ||
| 364 | } | ... | ... |
-
Please register or sign in to post a comment