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