mirror of
https://github.com/astaxie/beego.git
synced 2024-12-22 19:20:48 +00:00
support template
This commit is contained in:
parent
f502f84423
commit
19862725f7
@ -136,25 +136,32 @@ func (c *Controller) RenderBytes() ([]byte, error) {
|
||||
}
|
||||
err := BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data)
|
||||
if err != nil {
|
||||
LayoutTplErrDeal:
|
||||
if terr, ok := err.(*template.Error); ok {
|
||||
if terr.ErrorCode == template.ErrNoSuchTemplate {
|
||||
reg := regexp.MustCompile("\"(.+)\"")
|
||||
a := reg.FindStringSubmatch(terr.Description)
|
||||
reg := regexp.MustCompile("{{template \"(.+)\"")
|
||||
filedata, _ := ioutil.ReadFile(path.Join(ViewsPath, c.TplNames))
|
||||
a := reg.FindStringSubmatch(string(filedata))
|
||||
if len(a) > 1 {
|
||||
missfile := path.Join(ViewsPath, subdir, a[1])
|
||||
AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
|
||||
for _, tfile := range a[1:] {
|
||||
missfile := path.Join(ViewsPath, subdir, tfile)
|
||||
if ok, _ := FileExists(missfile); ok {
|
||||
AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
|
||||
}
|
||||
}
|
||||
for k, v := range AllTemplateFiles.files {
|
||||
BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...))
|
||||
}
|
||||
err = BeeTemplates[subdir].ExecuteTemplate(newbytes, file, c.Data)
|
||||
if err != nil {
|
||||
goto LayoutTplErrDeal
|
||||
Trace("template Execute err:", err)
|
||||
}
|
||||
goto LayoutTplOk
|
||||
}
|
||||
}
|
||||
}
|
||||
Trace("template Execute err:", err)
|
||||
}
|
||||
LayoutTplOk:
|
||||
tplcontent, _ := ioutil.ReadAll(newbytes)
|
||||
c.Data["LayoutContent"] = template.HTML(string(tplcontent))
|
||||
subdir = path.Dir(c.Layout)
|
||||
@ -162,26 +169,32 @@ func (c *Controller) RenderBytes() ([]byte, error) {
|
||||
ibytes := bytes.NewBufferString("")
|
||||
err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
|
||||
if err != nil {
|
||||
LayoutErrDeal:
|
||||
if terr, ok := err.(*template.Error); ok {
|
||||
if terr.ErrorCode == template.ErrNoSuchTemplate {
|
||||
reg := regexp.MustCompile("\"(.+)\"")
|
||||
a := reg.FindStringSubmatch(terr.Description)
|
||||
reg := regexp.MustCompile("{{template \"(.+)\"")
|
||||
filedata, _ := ioutil.ReadFile(path.Join(ViewsPath, c.Layout))
|
||||
a := reg.FindStringSubmatch(string(filedata))
|
||||
if len(a) > 1 {
|
||||
missfile := path.Join(ViewsPath, subdir, a[1])
|
||||
AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
|
||||
for _, tfile := range a[1:] {
|
||||
missfile := path.Join(ViewsPath, subdir, tfile)
|
||||
if ok, _ := FileExists(missfile); ok {
|
||||
AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
|
||||
}
|
||||
}
|
||||
for k, v := range AllTemplateFiles.files {
|
||||
BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...))
|
||||
}
|
||||
err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
|
||||
if err != nil {
|
||||
goto LayoutErrDeal
|
||||
Trace("template Execute err:", err)
|
||||
}
|
||||
goto LayoutOk
|
||||
}
|
||||
}
|
||||
}
|
||||
Trace("template Execute err:", err)
|
||||
}
|
||||
LayoutOk:
|
||||
icontent, _ := ioutil.ReadAll(ibytes)
|
||||
return icontent, nil
|
||||
} else {
|
||||
@ -200,26 +213,31 @@ func (c *Controller) RenderBytes() ([]byte, error) {
|
||||
}
|
||||
err := BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
|
||||
if err != nil {
|
||||
ErrDeal:
|
||||
if terr, ok := err.(*template.Error); ok {
|
||||
if terr.ErrorCode == template.ErrNoSuchTemplate {
|
||||
reg := regexp.MustCompile("\"(.+)\"")
|
||||
a := reg.FindStringSubmatch(terr.Description)
|
||||
reg := regexp.MustCompile("{{template \"(.+)\"")
|
||||
filedata, _ := ioutil.ReadFile(path.Join(ViewsPath, c.TplNames))
|
||||
a := reg.FindStringSubmatch(string(filedata))
|
||||
if len(a) > 1 {
|
||||
missfile := path.Join(ViewsPath, subdir, a[1])
|
||||
AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
|
||||
for _, tfile := range a[1:] {
|
||||
missfile := path.Join(ViewsPath, subdir, tfile)
|
||||
if ok, _ := FileExists(missfile); ok {
|
||||
AllTemplateFiles.files[subdir] = append(AllTemplateFiles.files[subdir], missfile)
|
||||
}
|
||||
}
|
||||
for k, v := range AllTemplateFiles.files {
|
||||
BeeTemplates[k] = template.Must(template.New("beegoTemplate"+k).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap).ParseFiles(v...))
|
||||
}
|
||||
err = BeeTemplates[subdir].ExecuteTemplate(ibytes, file, c.Data)
|
||||
if err != nil {
|
||||
goto ErrDeal
|
||||
Trace("template Execute err:", err)
|
||||
}
|
||||
goto TplOk
|
||||
}
|
||||
}
|
||||
}
|
||||
Trace("template Execute err:", err)
|
||||
}
|
||||
TplOk:
|
||||
icontent, _ := ioutil.ReadAll(ibytes)
|
||||
return icontent, nil
|
||||
}
|
||||
|
34
utils.go
34
utils.go
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@ -68,14 +69,14 @@ func DateFormat(t time.Time, layout string) (datestring string) {
|
||||
|
||||
var DatePatterns = []string{
|
||||
// year
|
||||
"Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
|
||||
"y", "06", //A two digit representation of a year Examples: 99 or 03
|
||||
"Y", "2006", // A full numeric representation of a year, 4 digits Examples: 1999 or 2003
|
||||
"y", "06", //A two digit representation of a year Examples: 99 or 03
|
||||
|
||||
// month
|
||||
"m", "01", // Numeric representation of a month, with leading zeros 01 through 12
|
||||
"n", "1", // Numeric representation of a month, without leading zeros 1 through 12
|
||||
"n", "1", // Numeric representation of a month, without leading zeros 1 through 12
|
||||
"M", "Jan", // A short textual representation of a month, three letters Jan through Dec
|
||||
"F", "January", // A full textual representation of a month, such as January or March January through December
|
||||
"F", "January", // A full textual representation of a month, such as January or March January through December
|
||||
|
||||
// day
|
||||
"d", "02", // Day of the month, 2 digits with leading zeros 01 to 31
|
||||
@ -83,19 +84,19 @@ var DatePatterns = []string{
|
||||
|
||||
// week
|
||||
"D", "Mon", // A textual representation of a day, three letters Mon through Sun
|
||||
"l", "Monday", // A full textual representation of the day of the week Sunday through Saturday
|
||||
"l", "Monday", // A full textual representation of the day of the week Sunday through Saturday
|
||||
|
||||
// time
|
||||
"g", "3", // 12-hour format of an hour without leading zeros 1 through 12
|
||||
"G", "15", // 24-hour format of an hour without leading zeros 0 through 23
|
||||
"h", "03", // 12-hour format of an hour with leading zeros 01 through 12
|
||||
"H", "15", // 24-hour format of an hour with leading zeros 00 through 23
|
||||
"g", "3", // 12-hour format of an hour without leading zeros 1 through 12
|
||||
"G", "15", // 24-hour format of an hour without leading zeros 0 through 23
|
||||
"h", "03", // 12-hour format of an hour with leading zeros 01 through 12
|
||||
"H", "15", // 24-hour format of an hour with leading zeros 00 through 23
|
||||
|
||||
"a", "pm", // Lowercase Ante meridiem and Post meridiem am or pm
|
||||
"A", "PM", // Uppercase Ante meridiem and Post meridiem AM or PM
|
||||
|
||||
"i", "04", // Minutes with leading zeros 00 to 59
|
||||
"s", "05", // Seconds, with leading zeros 00 through 59
|
||||
"i", "04", // Minutes with leading zeros 00 to 59
|
||||
"s", "05", // Seconds, with leading zeros 00 through 59
|
||||
|
||||
// time zone
|
||||
"T", "MST",
|
||||
@ -350,3 +351,14 @@ func stringsToJson(str string) string {
|
||||
}
|
||||
return jsons
|
||||
}
|
||||
|
||||
func FileExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user