ghttp_response_view.go 3.1 KB

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