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