1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 21:01:31 +00:00

增加编译模版函数BuildTemplate可变参数,使之支持单个或多个模版的编译,同时针对开发模式,每个请求只编译当前请求相关模版

增加编译模版函数BuildTemplate可变参数,使之支持单个或多个模版的编译,同时针对开发模式,每个请求只编译当前请求相关模版,不再每次请求都编译全部模版
This commit is contained in:
wulove 2015-08-06 10:09:34 +08:00
parent 57fdc308e3
commit 877b5c233e
3 changed files with 22 additions and 9 deletions

View File

@ -200,8 +200,19 @@ func (c *Controller) RenderBytes() ([]byte, error) {
if c.TplNames == "" { if c.TplNames == "" {
c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt
} }
if RunMode == "dev" { 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("") newbytes := bytes.NewBufferString("")
if _, ok := BeeTemplates[c.TplNames]; !ok { 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 c.TplNames = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt
} }
if RunMode == "dev" { if RunMode == "dev" {
BuildTemplate(ViewsPath) BuildTemplate(ViewsPath, c.TplNames)
} }
ibytes := bytes.NewBufferString("") ibytes := bytes.NewBufferString("")
if _, ok := BeeTemplates[c.TplNames]; !ok { if _, ok := BeeTemplates[c.TplNames]; !ok {

View File

@ -127,7 +127,7 @@ func AddTemplateExt(ext string) {
// build all template files in a directory. // build all template files in a directory.
// it makes beego can render any template file in view 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 _, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil return nil
@ -148,6 +148,7 @@ func BuildTemplate(dir string) error {
} }
for _, v := range self.files { for _, v := range self.files {
for _, file := range v { for _, file := range v {
if (files == nil || utils.InSlice(file, files)) {
t, err := getTemplate(self.root, file, v...) t, err := getTemplate(self.root, file, v...)
if err != nil { if err != nil {
Trace("parse template err:", file, err) Trace("parse template err:", file, err)
@ -156,6 +157,7 @@ func BuildTemplate(dir string) error {
} }
} }
} }
}
return nil return nil
} }

View File

@ -123,7 +123,7 @@ func TestRelativeTemplate(t *testing.T) {
f.Close() f.Close()
} }
} }
if err := BuildTemplate(dir); err != nil { if err := BuildTemplate(dir, files[1]); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := BeeTemplates["easyui/rbac/user.tpl"].ExecuteTemplate(os.Stdout, "easyui/rbac/user.tpl", nil); err != nil { if err := BeeTemplates["easyui/rbac/user.tpl"].ExecuteTemplate(os.Stdout, "easyui/rbac/user.tpl", nil); err != nil {