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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// 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
|
|
|
)
|
|
|
|
|
2019-11-20 12:09:26 +08:00
|
|
|
// WriteTpl parses and responses given template file.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The parameter `params` specifies the template variables for parsing.
|
2019-09-16 10:44:07 +08:00
|
|
|
func (r *Response) WriteTpl(tpl string, params ...gview.Params) error {
|
2021-12-17 17:42:18 +08:00
|
|
|
r.Header().Set("Content-Type", contentTypeHtml)
|
2021-11-16 00:26:10 +08:00
|
|
|
b, err := r.ParseTpl(tpl, params...)
|
|
|
|
if err != nil {
|
2019-09-16 11:08:41 +08:00
|
|
|
if !gmode.IsProduct() {
|
2019-09-16 10:44:07 +08:00
|
|
|
r.Write("Template Parsing Error: " + err.Error())
|
|
|
|
}
|
|
|
|
return err
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2021-11-16 00:26:10 +08:00
|
|
|
r.Write(b)
|
2019-09-16 10:44:07 +08:00
|
|
|
return nil
|
2018-08-23 21:55:27 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 12:09:26 +08:00
|
|
|
// WriteTplDefault parses and responses the default template file.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The parameter `params` specifies the template variables for parsing.
|
2019-11-20 12:09:26 +08:00
|
|
|
func (r *Response) WriteTplDefault(params ...gview.Params) error {
|
2021-12-17 17:42:18 +08:00
|
|
|
r.Header().Set("Content-Type", contentTypeHtml)
|
2021-11-16 00:26:10 +08:00
|
|
|
b, err := r.ParseTplDefault(params...)
|
|
|
|
if err != nil {
|
2019-11-20 12:09:26 +08:00
|
|
|
if !gmode.IsProduct() {
|
|
|
|
r.Write("Template Parsing Error: " + err.Error())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2021-11-16 00:26:10 +08:00
|
|
|
r.Write(b)
|
2019-11-20 12:09:26 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTplContent parses and responses the template content.
|
2021-10-21 18:22:47 +08:00
|
|
|
// The parameter `params` specifies the template variables for parsing.
|
2019-09-16 10:44:07 +08:00
|
|
|
func (r *Response) WriteTplContent(content string, params ...gview.Params) error {
|
2021-12-17 17:42:18 +08:00
|
|
|
r.Header().Set("Content-Type", contentTypeHtml)
|
2021-11-16 00:26:10 +08:00
|
|
|
b, err := r.ParseTplContent(content, params...)
|
|
|
|
if err != nil {
|
2019-09-16 11:08:41 +08:00
|
|
|
if !gmode.IsProduct() {
|
2019-09-16 10:44:07 +08:00
|
|
|
r.Write("Template Parsing Error: " + err.Error())
|
|
|
|
}
|
|
|
|
return err
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2021-11-16 00:26:10 +08:00
|
|
|
r.Write(b)
|
2019-09-16 10:44:07 +08:00
|
|
|
return nil
|
2018-09-04 22:32:43 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// ParseTpl parses given template file `tpl` with given template variables `params`
|
2019-11-20 12:09:26 +08:00
|
|
|
// 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) {
|
2021-05-13 00:16:45 +08:00
|
|
|
return r.Request.GetView().Parse(r.Request.Context(), tpl, r.buildInVars(params...))
|
2018-09-04 22:32:43 +08:00
|
|
|
}
|
|
|
|
|
2021-07-06 09:53:35 +08:00
|
|
|
// ParseTplDefault parses the default template file with params.
|
2019-11-20 12:09:26 +08:00
|
|
|
func (r *Response) ParseTplDefault(params ...gview.Params) (string, error) {
|
2021-05-13 00:16:45 +08:00
|
|
|
return r.Request.GetView().ParseDefault(r.Request.Context(), r.buildInVars(params...))
|
2019-11-20 12:09:26 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// ParseTplContent parses given template file `file` with given template parameters `params`
|
2019-11-20 12:09:26 +08:00
|
|
|
// 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) {
|
2021-05-13 00:16:45 +08:00
|
|
|
return r.Request.GetView().ParseContent(r.Request.Context(), content, r.buildInVars(params...))
|
2018-09-04 22:32:43 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// buildInVars merges build-in variables into `params` and returns the new template variables.
|
2020-11-14 10:24:06 +08:00
|
|
|
// TODO performance improving.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (r *Response) buildInVars(params ...map[string]interface{}) map[string]interface{} {
|
2020-11-14 10:24:06 +08:00
|
|
|
m := gutil.MapMergeCopy(r.Request.viewParams)
|
|
|
|
if len(params) > 0 {
|
|
|
|
gutil.MapMerge(m, params[0])
|
|
|
|
}
|
2019-11-20 18:45:09 +08:00
|
|
|
// Retrieve custom template variables from request object.
|
2021-09-27 21:27:24 +08:00
|
|
|
sessionMap := gconv.MapDeep(r.Request.Session.MustData())
|
2020-11-14 10:24:06 +08:00
|
|
|
gutil.MapMerge(m, map[string]interface{}{
|
2020-04-14 21:11:12 +08:00
|
|
|
"Form": r.Request.GetFormMap(),
|
|
|
|
"Query": r.Request.GetQueryMap(),
|
2021-01-02 01:53:36 +08:00
|
|
|
"Request": r.Request.GetMap(),
|
2020-04-14 21:11:12 +08:00
|
|
|
"Cookie": r.Request.Cookie.Map(),
|
2021-07-06 09:58:25 +08:00
|
|
|
"Session": sessionMap,
|
2020-04-14 21:11:12 +08:00
|
|
|
})
|
2019-11-20 18:45:09 +08:00
|
|
|
// Note that it should assign no Config variable to template
|
|
|
|
// if there's no configuration file.
|
2021-09-19 10:01:09 +08:00
|
|
|
if v, _ := gcfg.Instance().Data(r.Request.Context()); len(v) > 0 {
|
|
|
|
m["Config"] = v
|
2019-06-05 21:58:27 +08:00
|
|
|
}
|
2020-04-14 21:11:12 +08:00
|
|
|
return m
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|