/
ghttp_response_view.go
93 lines (84 loc) · 3.09 KB
/
ghttp_response_view.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2017 gf Author(https://github.com/csrgxtu/gf). All Rights Reserved.
//
// 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/csrgxtu/gf.
//
package ghttp
import (
"github.com/csrgxtu/gf/os/gcfg"
"github.com/csrgxtu/gf/os/gview"
"github.com/csrgxtu/gf/util/gmode"
"github.com/csrgxtu/gf/util/gutil"
)
// 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 {
if b, err := r.ParseTpl(tpl, params...); err != nil {
if !gmode.IsProduct() {
r.Write("Template Parsing Error: " + err.Error())
}
return err
} else {
r.Write(b)
}
return nil
}
// 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 {
if b, err := r.ParseTplContent(content, params...); err != nil {
if !gmode.IsProduct() {
r.Write("Template Parsing Error: " + err.Error())
}
return err
} else {
r.Write(b)
}
return nil
}
// ParseTpl parses given template file <tpl> with given template variables <params>
// and returns the parsed template content.
func (r *Response) ParseTpl(tpl string, params ...gview.Params) (string, error) {
return r.Request.GetView().Parse(tpl, r.buildInVars(params...))
}
// ParseDefault parses the default template file with params.
func (r *Response) ParseTplDefault(params ...gview.Params) (string, error) {
return r.Request.GetView().ParseDefault(r.buildInVars(params...))
}
// ParseTplContent parses given template file <file> with given template parameters <params>
// and returns the parsed template content.
func (r *Response) ParseTplContent(content string, params ...gview.Params) (string, error) {
return r.Request.GetView().ParseContent(content, r.buildInVars(params...))
}
// buildInVars merges build-in variables into <params> and returns the new template variables.
func (r *Response) buildInVars(params ...map[string]interface{}) map[string]interface{} {
m := gutil.MapMergeCopy(params...)
// Retrieve custom template variables from request object.
gutil.MapMerge(m, r.Request.viewParams, map[string]interface{}{
"Form": r.Request.GetFormMap(),
"Query": r.Request.GetQueryMap(),
"Request": r.Request.GetMap(),
"Cookie": r.Request.Cookie.Map(),
"Session": r.Request.Session.Map(),
})
// Note that it should assign no Config variable to template
// if there's no configuration file.
if c := gcfg.Instance(); c.Available() {
m["Config"] = c.GetMap(".")
}
return m
}