gview_buildin.go 8.6 KB

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