Merge branch 'master' of github.com:astaxie/beego
Showing
3 changed files
with
76 additions
and
26 deletions
| ... | @@ -115,7 +115,7 @@ | ... | @@ -115,7 +115,7 @@ |
| 115 | 115 | ||
| 116 | 2013/04/13 19:36:17 [W] [stat views: no such file or directory] | 116 | 2013/04/13 19:36:17 [W] [stat views: no such file or directory] |
| 117 | 117 | ||
| 118 | - 模板会自动重新加载不缓存。 | 118 | - 模板每次使用都会重新加载,不进行缓存。 |
| 119 | - 如果服务端出错,那么就会在浏览器端显示如下类似的截图: | 119 | - 如果服务端出错,那么就会在浏览器端显示如下类似的截图: |
| 120 | 120 | ||
| 121 |  | 121 |  |
| ... | @@ -643,11 +643,11 @@ beego更加人性化的还有一个设计就是支持用户自定义字符串错 | ... | @@ -643,11 +643,11 @@ beego更加人性化的还有一个设计就是支持用户自定义字符串错 |
| 643 | 643 | ||
| 644 | ## response处理 | 644 | ## response处理 |
| 645 | 645 | ||
| 646 | response可能会有集中情况: | 646 | response可能会有几种情况: |
| 647 | 647 | ||
| 648 | 1. 模板输出 | 648 | 1. 模板输出 |
| 649 | 649 | ||
| 650 | 模板输出上面模板介绍里面已经介绍,beego会在执行完相应的Controller里面的对应的Method之后输出到模板。 | 650 | 上面模板介绍里面已经介绍,beego会在执行完相应的Controller里面的对应的Method之后输出到模板。 |
| 651 | 651 | ||
| 652 | 2. 跳转 | 652 | 2. 跳转 |
| 653 | 653 | ||
| ... | @@ -686,7 +686,7 @@ beego中使用session相当方便,只要在main入口函数中设置如下: | ... | @@ -686,7 +686,7 @@ beego中使用session相当方便,只要在main入口函数中设置如下: |
| 686 | this.TplNames = "index.tpl" | 686 | this.TplNames = "index.tpl" |
| 687 | } | 687 | } |
| 688 | 688 | ||
| 689 | 上面的例子中我们知道session有几个方便的方法: | 689 | session有几个方便的方法: |
| 690 | 690 | ||
| 691 | - SetSession(name string, value interface{}) | 691 | - SetSession(name string, value interface{}) |
| 692 | - GetSession(name string) interface{} | 692 | - GetSession(name string) interface{} | ... | ... |
| ... | @@ -51,34 +51,35 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error { | ... | @@ -51,34 +51,35 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error { |
| 51 | if f == nil { | 51 | if f == nil { |
| 52 | return err | 52 | return err |
| 53 | } | 53 | } |
| 54 | if f.IsDir() { | 54 | if f.IsDir() || (f.Mode()&os.ModeSymlink) > 0 { |
| 55 | return nil | 55 | return nil |
| 56 | } else if (f.Mode() & os.ModeSymlink) > 0 { | 56 | } |
| 57 | if !HasTemplateEXt(paths) { | ||
| 57 | return nil | 58 | return nil |
| 59 | } | ||
| 60 | |||
| 61 | replace := strings.NewReplacer("\\", "/") | ||
| 62 | a := []byte(paths) | ||
| 63 | a = a[len([]byte(self.root)):] | ||
| 64 | subdir := path.Dir(strings.TrimLeft(replace.Replace(string(a)), "/")) | ||
| 65 | if _, ok := self.files[subdir]; ok { | ||
| 66 | self.files[subdir] = append(self.files[subdir], paths) | ||
| 58 | } else { | 67 | } else { |
| 59 | hasExt := false | 68 | m := make([]string, 1) |
| 60 | for _, v := range BeeTemplateExt { | 69 | m[0] = paths |
| 61 | if strings.HasSuffix(paths, v) { | 70 | self.files[subdir] = m |
| 62 | hasExt = true | 71 | } |
| 63 | break | ||
| 64 | } | ||
| 65 | } | ||
| 66 | if hasExt { | ||
| 67 | replace := strings.NewReplacer("\\", "/") | ||
| 68 | a := []byte(paths) | ||
| 69 | a = a[len([]byte(self.root)):] | ||
| 70 | subdir := path.Dir(strings.TrimLeft(replace.Replace(string(a)), "/")) | ||
| 71 | if _, ok := self.files[subdir]; ok { | ||
| 72 | self.files[subdir] = append(self.files[subdir], paths) | ||
| 73 | } else { | ||
| 74 | m := make([]string, 1) | ||
| 75 | m[0] = paths | ||
| 76 | self.files[subdir] = m | ||
| 77 | } | ||
| 78 | 72 | ||
| 73 | return nil | ||
| 74 | } | ||
| 75 | |||
| 76 | func HasTemplateEXt(paths string) bool { | ||
| 77 | for _, v := range BeeTemplateExt { | ||
| 78 | if strings.HasSuffix(paths, "."+v) { | ||
| 79 | return true | ||
| 79 | } | 80 | } |
| 80 | } | 81 | } |
| 81 | return nil | 82 | return false |
| 82 | } | 83 | } |
| 83 | 84 | ||
| 84 | func AddTemplateExt(ext string) { | 85 | func AddTemplateExt(ext string) { | ... | ... |
template_test.go
0 → 100644
| 1 | package beego | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "os" | ||
| 5 | "path/filepath" | ||
| 6 | "testing" | ||
| 7 | ) | ||
| 8 | |||
| 9 | func TestBuildTemplate(t *testing.T) { | ||
| 10 | dir := "_beeTmp" | ||
| 11 | files := []string{ | ||
| 12 | "1.tpl", | ||
| 13 | "2.html", | ||
| 14 | "3.htmltpl", | ||
| 15 | "4.mystyle", | ||
| 16 | } | ||
| 17 | if err := os.MkdirAll(dir, 0777); err != nil { | ||
| 18 | t.Fatal(err) | ||
| 19 | } | ||
| 20 | for _, name := range files { | ||
| 21 | if _, err := os.Create(filepath.Join(dir, name)); err != nil { | ||
| 22 | t.Fatal(err) | ||
| 23 | } | ||
| 24 | } | ||
| 25 | if err := BuildTemplate(dir); err != nil { | ||
| 26 | t.Fatal(err) | ||
| 27 | } | ||
| 28 | if len(BeeTemplates) != 1 { | ||
| 29 | t.Fatalf("should be 1 but got %v", len(BeeTemplates)) | ||
| 30 | } | ||
| 31 | for _, v := range BeeTemplates { | ||
| 32 | if len(v.Templates()) != 3 { | ||
| 33 | t.Errorf("should be 3 but got %v", len(v.Templates())) | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | AddTemplateExt("mystyle") | ||
| 38 | if err := BuildTemplate(dir); err != nil { | ||
| 39 | t.Fatal(err) | ||
| 40 | } | ||
| 41 | if len(BeeTemplates) != 1 { | ||
| 42 | t.Fatalf("should be 1 but got %v", len(BeeTemplates)) | ||
| 43 | } | ||
| 44 | for _, v := range BeeTemplates { | ||
| 45 | if len(v.Templates()) != 4 { | ||
| 46 | t.Errorf("should be 4 but got %v", len(v.Templates())) | ||
| 47 | } | ||
| 48 | } | ||
| 49 | } |
-
Please register or sign in to post a comment