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