gf/net/ghttp/ghttp_response_view.go

100 lines
3.3 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2018-08-23 21:55:27 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2018-08-23 21:55:27 +08:00
//
package ghttp
import (
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/os/gview"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gmode"
"github.com/gogf/gf/v2/util/gutil"
2018-08-23 21:55:27 +08:00
)
// WriteTpl parses and responses given template file.
// The parameter `params` specifies the template variables for parsing.
func (r *Response) WriteTpl(tpl string, params ...gview.Params) error {
2019-06-19 09:06:52 +08:00
if b, err := r.ParseTpl(tpl, params...); err != nil {
if !gmode.IsProduct() {
r.Write("Template Parsing Error: " + err.Error())
}
return err
2019-06-19 09:06:52 +08:00
} else {
r.Write(b)
}
return nil
2018-08-23 21:55:27 +08:00
}
// WriteTplDefault parses and responses the default template file.
// The parameter `params` specifies the template variables for parsing.
func (r *Response) WriteTplDefault(params ...gview.Params) error {
if b, err := r.ParseTplDefault(params...); err != nil {
if !gmode.IsProduct() {
r.Write("Template Parsing Error: " + err.Error())
}
return err
} else {
r.Write(b)
}
return nil
}
// WriteTplContent parses and responses the template content.
// The parameter `params` specifies the template variables for parsing.
func (r *Response) WriteTplContent(content string, params ...gview.Params) error {
2019-06-19 09:06:52 +08:00
if b, err := r.ParseTplContent(content, params...); err != nil {
if !gmode.IsProduct() {
r.Write("Template Parsing Error: " + err.Error())
}
return err
2019-06-19 09:06:52 +08:00
} else {
r.Write(b)
}
return nil
}
// ParseTpl parses given template file `tpl` with given template variables `params`
// and returns the parsed template content.
2019-06-19 09:06:52 +08:00
func (r *Response) ParseTpl(tpl string, params ...gview.Params) (string, error) {
return r.Request.GetView().Parse(r.Request.Context(), tpl, r.buildInVars(params...))
}
// ParseTplDefault parses the default template file with params.
func (r *Response) ParseTplDefault(params ...gview.Params) (string, error) {
return r.Request.GetView().ParseDefault(r.Request.Context(), r.buildInVars(params...))
}
// ParseTplContent parses given template file `file` with given template parameters `params`
// and returns the parsed template content.
2019-06-19 09:06:52 +08:00
func (r *Response) ParseTplContent(content string, params ...gview.Params) (string, error) {
return r.Request.GetView().ParseContent(r.Request.Context(), content, r.buildInVars(params...))
}
// buildInVars merges build-in variables into `params` and returns the new template variables.
// TODO performance improving.
2019-06-19 09:06:52 +08:00
func (r *Response) buildInVars(params ...map[string]interface{}) map[string]interface{} {
m := gutil.MapMergeCopy(r.Request.viewParams)
if len(params) > 0 {
gutil.MapMerge(m, params[0])
}
// Retrieve custom template variables from request object.
2021-09-27 21:27:24 +08:00
sessionMap := gconv.MapDeep(r.Request.Session.MustData())
gutil.MapMerge(m, map[string]interface{}{
"Form": r.Request.GetFormMap(),
"Query": r.Request.GetQueryMap(),
"Request": r.Request.GetMap(),
"Cookie": r.Request.Cookie.Map(),
"Session": sessionMap,
})
// Note that it should assign no Config variable to template
// if there's no configuration file.
if v, _ := gcfg.Instance().Data(r.Request.Context()); len(v) > 0 {
m["Config"] = v
2019-06-05 21:58:27 +08:00
}
return m
2019-06-19 09:06:52 +08:00
}