gview_buildin.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/gogf/gf.
  6. package gview
  7. import (
  8. "fmt"
  9. "github.com/gogf/gf/internal/json"
  10. "github.com/gogf/gf/util/gutil"
  11. "strings"
  12. "github.com/gogf/gf/encoding/ghtml"
  13. "github.com/gogf/gf/encoding/gurl"
  14. "github.com/gogf/gf/os/gtime"
  15. "github.com/gogf/gf/text/gstr"
  16. "github.com/gogf/gf/util/gconv"
  17. htmltpl "html/template"
  18. )
  19. // buildInFuncDump implements build-in template function: dump
  20. func (view *View) buildInFuncDump(values ...interface{}) (result string) {
  21. result += "<!--\n"
  22. for _, v := range values {
  23. result += gutil.Export(v) + "\n"
  24. }
  25. result += "-->\n"
  26. return result
  27. }
  28. // buildInFuncMap implements build-in template function: map
  29. func (view *View) buildInFuncMap(value ...interface{}) map[string]interface{} {
  30. if len(value) > 0 {
  31. return gconv.Map(value[0])
  32. }
  33. return map[string]interface{}{}
  34. }
  35. // buildInFuncMaps implements build-in template function: maps
  36. func (view *View) buildInFuncMaps(value ...interface{}) []map[string]interface{} {
  37. if len(value) > 0 {
  38. return gconv.Maps(value[0])
  39. }
  40. return []map[string]interface{}{}
  41. }
  42. // buildInFuncEq implements build-in template function: eq
  43. func (view *View) buildInFuncEq(value interface{}, others ...interface{}) bool {
  44. s := gconv.String(value)
  45. for _, v := range others {
  46. if strings.Compare(s, gconv.String(v)) == 0 {
  47. return true
  48. }
  49. }
  50. return false
  51. }
  52. // buildInFuncNe implements build-in template function: ne
  53. func (view *View) buildInFuncNe(value, other interface{}) bool {
  54. return strings.Compare(gconv.String(value), gconv.String(other)) != 0
  55. }
  56. // buildInFuncLt implements build-in template function: lt
  57. func (view *View) buildInFuncLt(value, other interface{}) bool {
  58. s1 := gconv.String(value)
  59. s2 := gconv.String(other)
  60. if gstr.IsNumeric(s1) && gstr.IsNumeric(s2) {
  61. return gconv.Int64(value) < gconv.Int64(other)
  62. }
  63. return strings.Compare(s1, s2) < 0
  64. }
  65. // buildInFuncLe implements build-in template function: le
  66. func (view *View) buildInFuncLe(value, other interface{}) bool {
  67. s1 := gconv.String(value)
  68. s2 := gconv.String(other)
  69. if gstr.IsNumeric(s1) && gstr.IsNumeric(s2) {
  70. return gconv.Int64(value) <= gconv.Int64(other)
  71. }
  72. return strings.Compare(s1, s2) <= 0
  73. }
  74. // buildInFuncGt implements build-in template function: gt
  75. func (view *View) buildInFuncGt(value, other interface{}) bool {
  76. s1 := gconv.String(value)
  77. s2 := gconv.String(other)
  78. if gstr.IsNumeric(s1) && gstr.IsNumeric(s2) {
  79. return gconv.Int64(value) > gconv.Int64(other)
  80. }
  81. return strings.Compare(s1, s2) > 0
  82. }
  83. // buildInFuncGe implements build-in template function: ge
  84. func (view *View) buildInFuncGe(value, other interface{}) bool {
  85. s1 := gconv.String(value)
  86. s2 := gconv.String(other)
  87. if gstr.IsNumeric(s1) && gstr.IsNumeric(s2) {
  88. return gconv.Int64(value) >= gconv.Int64(other)
  89. }
  90. return strings.Compare(s1, s2) >= 0
  91. }
  92. // buildInFuncInclude implements build-in template function: include
  93. // Note that configuration AutoEncode does not affect the output of this function.
  94. func (view *View) buildInFuncInclude(file interface{}, data ...map[string]interface{}) htmltpl.HTML {
  95. var m map[string]interface{} = nil
  96. if len(data) > 0 {
  97. m = data[0]
  98. }
  99. path := gconv.String(file)
  100. if path == "" {
  101. return ""
  102. }
  103. // It will search the file internally.
  104. content, err := view.Parse(path, m)
  105. if err != nil {
  106. return htmltpl.HTML(err.Error())
  107. }
  108. return htmltpl.HTML(content)
  109. }
  110. // buildInFuncText implements build-in template function: text
  111. func (view *View) buildInFuncText(html interface{}) string {
  112. return ghtml.StripTags(gconv.String(html))
  113. }
  114. // buildInFuncHtmlEncode implements build-in template function: html
  115. func (view *View) buildInFuncHtmlEncode(html interface{}) string {
  116. return ghtml.Entities(gconv.String(html))
  117. }
  118. // buildInFuncHtmlDecode implements build-in template function: htmldecode
  119. func (view *View) buildInFuncHtmlDecode(html interface{}) string {
  120. return ghtml.EntitiesDecode(gconv.String(html))
  121. }
  122. // buildInFuncUrlEncode implements build-in template function: url
  123. func (view *View) buildInFuncUrlEncode(url interface{}) string {
  124. return gurl.Encode(gconv.String(url))
  125. }
  126. // buildInFuncUrlDecode implements build-in template function: urldecode
  127. func (view *View) buildInFuncUrlDecode(url interface{}) string {
  128. if content, err := gurl.Decode(gconv.String(url)); err == nil {
  129. return content
  130. } else {
  131. return err.Error()
  132. }
  133. }
  134. // buildInFuncDate implements build-in template function: date
  135. func (view *View) buildInFuncDate(format interface{}, timestamp ...interface{}) string {
  136. t := int64(0)
  137. if len(timestamp) > 0 {
  138. t = gconv.Int64(timestamp[0])
  139. }
  140. if t == 0 {
  141. t = gtime.Timestamp()
  142. }
  143. return gtime.NewFromTimeStamp(t).Format(gconv.String(format))
  144. }
  145. // buildInFuncCompare implements build-in template function: compare
  146. func (view *View) buildInFuncCompare(value1, value2 interface{}) int {
  147. return strings.Compare(gconv.String(value1), gconv.String(value2))
  148. }
  149. // buildInFuncSubStr implements build-in template function: substr
  150. func (view *View) buildInFuncSubStr(start, end, str interface{}) string {
  151. return gstr.SubStrRune(gconv.String(str), gconv.Int(start), gconv.Int(end))
  152. }
  153. // buildInFuncStrLimit implements build-in template function: strlimit
  154. func (view *View) buildInFuncStrLimit(length, suffix, str interface{}) string {
  155. return gstr.StrLimitRune(gconv.String(str), gconv.Int(length), gconv.String(suffix))
  156. }
  157. // buildInFuncConcat implements build-in template function: concat
  158. func (view *View) buildInFuncConcat(str ...interface{}) string {
  159. var s string
  160. for _, v := range str {
  161. s += gconv.String(v)
  162. }
  163. return s
  164. }
  165. // buildInFuncReplace implements build-in template function: replace
  166. func (view *View) buildInFuncReplace(search, replace, str interface{}) string {
  167. return gstr.Replace(gconv.String(str), gconv.String(search), gconv.String(replace), -1)
  168. }
  169. // buildInFuncHighlight implements build-in template function: highlight
  170. func (view *View) buildInFuncHighlight(key, color, str interface{}) string {
  171. return gstr.Replace(gconv.String(str), gconv.String(key), fmt.Sprintf(`<span style="color:%v;">%v</span>`, color, key))
  172. }
  173. // buildInFuncHideStr implements build-in template function: hidestr
  174. func (view *View) buildInFuncHideStr(percent, hide, str interface{}) string {
  175. return gstr.HideStr(gconv.String(str), gconv.Int(percent), gconv.String(hide))
  176. }
  177. // buildInFuncToUpper implements build-in template function: toupper
  178. func (view *View) buildInFuncToUpper(str interface{}) string {
  179. return gstr.ToUpper(gconv.String(str))
  180. }
  181. // buildInFuncToLower implements build-in template function: toupper
  182. func (view *View) buildInFuncToLower(str interface{}) string {
  183. return gstr.ToLower(gconv.String(str))
  184. }
  185. // buildInFuncNl2Br implements build-in template function: nl2br
  186. func (view *View) buildInFuncNl2Br(str interface{}) string {
  187. return gstr.Nl2Br(gconv.String(str))
  188. }
  189. // buildInFuncJson implements build-in template function: json ,
  190. // which encodes and returns <value> as JSON string.
  191. func (view *View) buildInFuncJson(value interface{}) (string, error) {
  192. b, err := json.Marshal(value)
  193. return gconv.UnsafeBytesToStr(b), err
  194. }