github.com/gogf/gf@v1.16.9/frame/gmvc/view.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gmvc 8 9 import ( 10 "github.com/gogf/gf/frame/gins" 11 "sync" 12 13 "github.com/gogf/gf/util/gmode" 14 15 "github.com/gogf/gf/net/ghttp" 16 "github.com/gogf/gf/os/gview" 17 ) 18 19 // View is the view object for controller. 20 // It's initialized when controller request initializes and destroyed 21 // when the controller request closes. 22 // Deprecated, no longer suggested. 23 type View struct { 24 mu sync.RWMutex 25 view *gview.View 26 data gview.Params 27 response *ghttp.Response 28 } 29 30 // NewView creates and returns a controller view object. 31 // Deprecated, no longer suggested. 32 func NewView(w *ghttp.Response) *View { 33 return &View{ 34 view: gins.View(), 35 data: make(gview.Params), 36 response: w, 37 } 38 } 39 40 // Assigns assigns template variables to this view object. 41 func (view *View) Assigns(data gview.Params) { 42 view.mu.Lock() 43 for k, v := range data { 44 view.data[k] = v 45 } 46 view.mu.Unlock() 47 } 48 49 // Assign assigns one template variable to this view object. 50 func (view *View) Assign(key string, value interface{}) { 51 view.mu.Lock() 52 view.data[key] = value 53 view.mu.Unlock() 54 } 55 56 // Parse parses given template file <tpl> with assigned template variables 57 // and returns the parsed template content. 58 func (view *View) Parse(file string) (string, error) { 59 view.mu.RLock() 60 defer view.mu.RUnlock() 61 buffer, err := view.response.ParseTpl(file, view.data) 62 return buffer, err 63 } 64 65 // ParseContent parses given template file <file> with assigned template variables 66 // and returns the parsed template content. 67 func (view *View) ParseContent(content string) (string, error) { 68 view.mu.RLock() 69 defer view.mu.RUnlock() 70 buffer, err := view.response.ParseTplContent(content, view.data) 71 return buffer, err 72 } 73 74 // LockFunc locks writing for template variables by callback function <f>. 75 func (view *View) LockFunc(f func(data gview.Params)) { 76 view.mu.Lock() 77 defer view.mu.Unlock() 78 f(view.data) 79 } 80 81 // RLockFunc locks reading for template variables by callback function <f>. 82 func (view *View) RLockFunc(f func(data gview.Params)) { 83 view.mu.RLock() 84 defer view.mu.RUnlock() 85 f(view.data) 86 } 87 88 // BindFunc registers customized template function named <name> 89 // with given function <function> to current view object. 90 // The <name> is the function name which can be called in template content. 91 func (view *View) BindFunc(name string, function interface{}) { 92 view.view.BindFunc(name, function) 93 } 94 95 // BindFuncMap registers customized template functions by map to current view object. 96 // The key of map is the template function name 97 // and the value of map is the address of customized function. 98 func (view *View) BindFuncMap(funcMap gview.FuncMap) { 99 view.view.BindFuncMap(funcMap) 100 } 101 102 // Display parses and writes the parsed template file content to http response. 103 func (view *View) Display(file ...string) error { 104 name := view.view.GetDefaultFile() 105 if len(file) > 0 { 106 name = file[0] 107 } 108 if content, err := view.Parse(name); err != nil { 109 if !gmode.IsProduct() { 110 view.response.Write("Tpl Parsing Error: " + err.Error()) 111 } 112 return err 113 } else { 114 view.response.Write(content) 115 } 116 return nil 117 } 118 119 // DisplayContent parses and writes the parsed content to http response. 120 func (view *View) DisplayContent(content string) error { 121 if content, err := view.ParseContent(content); err != nil { 122 if !gmode.IsProduct() { 123 view.response.Write("Tpl Parsing Error: " + err.Error()) 124 } 125 return err 126 } else { 127 view.response.Write(content) 128 } 129 return nil 130 }