github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_response_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 8 package ghttp 9 10 import ( 11 "github.com/gogf/gf/os/gcfg" 12 "github.com/gogf/gf/os/gview" 13 "github.com/gogf/gf/util/gconv" 14 "github.com/gogf/gf/util/gmode" 15 "github.com/gogf/gf/util/gutil" 16 ) 17 18 // WriteTpl parses and responses given template file. 19 // The parameter <params> specifies the template variables for parsing. 20 func (r *Response) WriteTpl(tpl string, params ...gview.Params) error { 21 if b, err := r.ParseTpl(tpl, params...); err != nil { 22 if !gmode.IsProduct() { 23 r.Write("Template Parsing Error: " + err.Error()) 24 } 25 return err 26 } else { 27 r.Write(b) 28 } 29 return nil 30 } 31 32 // WriteTplDefault parses and responses the default template file. 33 // The parameter <params> specifies the template variables for parsing. 34 func (r *Response) WriteTplDefault(params ...gview.Params) error { 35 if b, err := r.ParseTplDefault(params...); err != nil { 36 if !gmode.IsProduct() { 37 r.Write("Template Parsing Error: " + err.Error()) 38 } 39 return err 40 } else { 41 r.Write(b) 42 } 43 return nil 44 } 45 46 // WriteTplContent parses and responses the template content. 47 // The parameter <params> specifies the template variables for parsing. 48 func (r *Response) WriteTplContent(content string, params ...gview.Params) error { 49 if b, err := r.ParseTplContent(content, params...); err != nil { 50 if !gmode.IsProduct() { 51 r.Write("Template Parsing Error: " + err.Error()) 52 } 53 return err 54 } else { 55 r.Write(b) 56 } 57 return nil 58 } 59 60 // ParseTpl parses given template file <tpl> with given template variables <params> 61 // and returns the parsed template content. 62 func (r *Response) ParseTpl(tpl string, params ...gview.Params) (string, error) { 63 return r.Request.GetView().Parse(r.Request.Context(), tpl, r.buildInVars(params...)) 64 } 65 66 // ParseTplDefault parses the default template file with params. 67 func (r *Response) ParseTplDefault(params ...gview.Params) (string, error) { 68 return r.Request.GetView().ParseDefault(r.Request.Context(), r.buildInVars(params...)) 69 } 70 71 // ParseTplContent parses given template file <file> with given template parameters <params> 72 // and returns the parsed template content. 73 func (r *Response) ParseTplContent(content string, params ...gview.Params) (string, error) { 74 return r.Request.GetView().ParseContent(r.Request.Context(), content, r.buildInVars(params...)) 75 } 76 77 // buildInVars merges build-in variables into <params> and returns the new template variables. 78 // TODO performance improving. 79 func (r *Response) buildInVars(params ...map[string]interface{}) map[string]interface{} { 80 m := gutil.MapMergeCopy(r.Request.viewParams) 81 if len(params) > 0 { 82 gutil.MapMerge(m, params[0]) 83 } 84 // Retrieve custom template variables from request object. 85 sessionMap := gconv.MapDeep(r.Request.Session.Map()) 86 gutil.MapMerge(m, map[string]interface{}{ 87 "Form": r.Request.GetFormMap(), 88 "Query": r.Request.GetQueryMap(), 89 "Request": r.Request.GetMap(), 90 "Cookie": r.Request.Cookie.Map(), 91 "Session": sessionMap, 92 }) 93 // Note that it should assign no Config variable to template 94 // if there's no configuration file. 95 if c := gcfg.Instance(); c.Available() { 96 m["Config"] = c.Map() 97 } 98 return m 99 }