From 57fdc308e34e7b656585f1b56fb47078e10963c5 Mon Sep 17 00:00:00 2001 From: wulove Date: Thu, 6 Aug 2015 09:36:43 +0800 Subject: [PATCH 1/3] =?UTF-8?q?AutoRender=E4=B8=BA=E7=A9=BA=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E4=B8=8D=E5=86=8D=E7=BC=96=E8=AF=91=E6=A8=A1=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AutoRender为空,Controller.Render()不再执行,故无需编译模版 --- beego.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/beego.go b/beego.go index 60483764..7bcc0740 100644 --- a/beego.go +++ b/beego.go @@ -306,11 +306,13 @@ func initBeforeHttpRun() { } go GlobalSessions.GC() } - - err := BuildTemplate(ViewsPath) - if err != nil { - if RunMode == "dev" { - Warn(err) + + if AutoRender { + err := BuildTemplate(ViewsPath) + if err != nil { + if RunMode == "dev" { + Warn(err) + } } } From 877b5c233e889046853b17e799fa65e7aee1ee6b Mon Sep 17 00:00:00 2001 From: wulove Date: Thu, 6 Aug 2015 10:09:34 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E6=A8=A1=E7=89=88=E5=87=BD=E6=95=B0BuildTemplate=E5=8F=AF?= =?UTF-8?q?=E5=8F=98=E5=8F=82=E6=95=B0=EF=BC=8C=E4=BD=BF=E4=B9=8B=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=8D=95=E4=B8=AA=E6=88=96=E5=A4=9A=E4=B8=AA=E6=A8=A1?= =?UTF-8?q?=E7=89=88=E7=9A=84=E7=BC=96=E8=AF=91=EF=BC=8C=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E9=92=88=E5=AF=B9=E5=BC=80=E5=8F=91=E6=A8=A1=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=E8=AF=B7=E6=B1=82=E5=8F=AA=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E8=AF=B7=E6=B1=82=E7=9B=B8=E5=85=B3=E6=A8=A1?= =?UTF-8?q?=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加编译模版函数BuildTemplate可变参数,使之支持单个或多个模版的编译,同时针对开发模式,每个请求只编译当前请求相关模版,不再每次请求都编译全部模版 --- controller.go | 15 +++++++++++++-- template.go | 14 ++++++++------ template_test.go | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/controller.go b/controller.go index a8b9e8d3..65b311b6 100644 --- a/controller.go +++ b/controller.go @@ -200,8 +200,19 @@ func (c *Controller) RenderBytes() ([]byte, error) { if c.TplNames == "" { c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt } + if RunMode == "dev" { - BuildTemplate(ViewsPath) + buildFiles := make([]string, 1) + buildFiles = append(buildFiles, c.TplNames) + if c.LayoutSections != nil { + for _, sectionTpl := range c.LayoutSections { + if sectionTpl == "" { + continue + } + buildFiles = append(buildFiles, sectionTpl) + } + } + BuildTemplate(ViewsPath, buildFiles...) } newbytes := bytes.NewBufferString("") if _, ok := BeeTemplates[c.TplNames]; !ok { @@ -246,7 +257,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt } if RunMode == "dev" { - BuildTemplate(ViewsPath) + BuildTemplate(ViewsPath, c.TplNames) } ibytes := bytes.NewBufferString("") if _, ok := BeeTemplates[c.TplNames]; !ok { diff --git a/template.go b/template.go index 64b1939e..45ecbe52 100644 --- a/template.go +++ b/template.go @@ -127,7 +127,7 @@ func AddTemplateExt(ext string) { // build all template files in a directory. // it makes beego can render any template file in view directory. -func BuildTemplate(dir string) error { +func BuildTemplate(dir string, files... string) error { if _, err := os.Stat(dir); err != nil { if os.IsNotExist(err) { return nil @@ -148,11 +148,13 @@ func BuildTemplate(dir string) error { } for _, v := range self.files { for _, file := range v { - t, err := getTemplate(self.root, file, v...) - if err != nil { - Trace("parse template err:", file, err) - } else { - BeeTemplates[file] = t + if (files == nil || utils.InSlice(file, files)) { + t, err := getTemplate(self.root, file, v...) + if err != nil { + Trace("parse template err:", file, err) + } else { + BeeTemplates[file] = t + } } } } diff --git a/template_test.go b/template_test.go index b35da5ce..79740f2b 100644 --- a/template_test.go +++ b/template_test.go @@ -123,7 +123,7 @@ func TestRelativeTemplate(t *testing.T) { f.Close() } } - if err := BuildTemplate(dir); err != nil { + if err := BuildTemplate(dir, files[1]); err != nil { t.Fatal(err) } if err := BeeTemplates["easyui/rbac/user.tpl"].ExecuteTemplate(os.Stdout, "easyui/rbac/user.tpl", nil); err != nil { From 38ddb61199f7ab90bfa7265a3b6cd68ecfb81c76 Mon Sep 17 00:00:00 2001 From: wulove Date: Fri, 21 Aug 2015 10:32:53 +0800 Subject: [PATCH 3/3] files is []string, use len(files)==0 --- template.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template.go b/template.go index 45ecbe52..32468d77 100644 --- a/template.go +++ b/template.go @@ -148,7 +148,7 @@ func BuildTemplate(dir string, files... string) error { } for _, v := range self.files { for _, file := range v { - if (files == nil || utils.InSlice(file, files)) { + if (len(files) == 0 || utils.InSlice(file, files)) { t, err := getTemplate(self.root, file, v...) if err != nil { Trace("parse template err:", file, err)