github.com/zhongdalu/gf@v1.0.0/g/os/gview/gview_buildin.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/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/zhongdalu/gf.
     6  
     7  package gview
     8  
     9  import (
    10  	"fmt"
    11  	"github.com/zhongdalu/gf/g/encoding/ghtml"
    12  	"github.com/zhongdalu/gf/g/encoding/gurl"
    13  	"github.com/zhongdalu/gf/g/os/gtime"
    14  	"github.com/zhongdalu/gf/g/text/gstr"
    15  	"github.com/zhongdalu/gf/g/util/gconv"
    16  	"strings"
    17  )
    18  
    19  // Build-in template function: eq
    20  func (view *View) funcEq(value interface{}, others ...interface{}) bool {
    21  	s := gconv.String(value)
    22  	for _, v := range others {
    23  		if strings.Compare(s, gconv.String(v)) != 0 {
    24  			return false
    25  		}
    26  	}
    27  	return true
    28  }
    29  
    30  // Build-in template function: ne
    31  func (view *View) funcNe(value interface{}, other interface{}) bool {
    32  	return strings.Compare(gconv.String(value), gconv.String(other)) != 0
    33  }
    34  
    35  // Build-in template function: lt
    36  func (view *View) funcLt(value interface{}, other interface{}) bool {
    37  	s1 := gconv.String(value)
    38  	s2 := gconv.String(other)
    39  	if gstr.IsNumeric(s1) && gstr.IsNumeric(s2) {
    40  		return gconv.Int64(value) < gconv.Int64(other)
    41  	}
    42  	return strings.Compare(s1, s2) < 0
    43  }
    44  
    45  // Build-in template function: le
    46  func (view *View) funcLe(value interface{}, other interface{}) bool {
    47  	s1 := gconv.String(value)
    48  	s2 := gconv.String(other)
    49  	if gstr.IsNumeric(s1) && gstr.IsNumeric(s2) {
    50  		return gconv.Int64(value) <= gconv.Int64(other)
    51  	}
    52  	return strings.Compare(s1, s2) <= 0
    53  }
    54  
    55  // Build-in template function: gt
    56  func (view *View) funcGt(value interface{}, other interface{}) bool {
    57  	s1 := gconv.String(value)
    58  	s2 := gconv.String(other)
    59  	if gstr.IsNumeric(s1) && gstr.IsNumeric(s2) {
    60  		return gconv.Int64(value) > gconv.Int64(other)
    61  	}
    62  	return strings.Compare(s1, s2) > 0
    63  }
    64  
    65  // Build-in template function: ge
    66  func (view *View) funcGe(value interface{}, 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  
    75  // Build-in template function: include
    76  func (view *View) funcInclude(file string, data ...map[string]interface{}) string {
    77  	var m map[string]interface{} = nil
    78  	if len(data) > 0 {
    79  		m = data[0]
    80  	}
    81  	content, err := view.Parse(file, m)
    82  	if err != nil {
    83  		return err.Error()
    84  	}
    85  	return content
    86  }
    87  
    88  // Build-in template function: text
    89  func (view *View) funcText(html interface{}) string {
    90  	return ghtml.StripTags(gconv.String(html))
    91  }
    92  
    93  // Build-in template function: html
    94  func (view *View) funcHtmlEncode(html interface{}) string {
    95  	return ghtml.Entities(gconv.String(html))
    96  }
    97  
    98  // Build-in template function: htmldecode
    99  func (view *View) funcHtmlDecode(html interface{}) string {
   100  	return ghtml.EntitiesDecode(gconv.String(html))
   101  }
   102  
   103  // Build-in template function: url
   104  func (view *View) funcUrlEncode(url interface{}) string {
   105  	return gurl.Encode(gconv.String(url))
   106  }
   107  
   108  // Build-in template function: urldecode
   109  func (view *View) funcUrlDecode(url interface{}) string {
   110  	if content, err := gurl.Decode(gconv.String(url)); err == nil {
   111  		return content
   112  	} else {
   113  		return err.Error()
   114  	}
   115  }
   116  
   117  // Build-in template function: date
   118  func (view *View) funcDate(format string, timestamp ...interface{}) string {
   119  	t := int64(0)
   120  	if len(timestamp) > 0 {
   121  		t = gconv.Int64(timestamp[0])
   122  	}
   123  	if t == 0 {
   124  		t = gtime.Millisecond()
   125  	}
   126  	return gtime.NewFromTimeStamp(t).Format(format)
   127  }
   128  
   129  // Build-in template function: compare
   130  func (view *View) funcCompare(value1, value2 interface{}) int {
   131  	return strings.Compare(gconv.String(value1), gconv.String(value2))
   132  }
   133  
   134  // Build-in template function: substr
   135  func (view *View) funcSubStr(start, end int, str interface{}) string {
   136  	return gstr.SubStr(gconv.String(str), start, end)
   137  }
   138  
   139  // Build-in template function: strlimit
   140  func (view *View) funcStrLimit(length int, suffix string, str interface{}) string {
   141  	return gstr.StrLimit(gconv.String(str), length, suffix)
   142  }
   143  
   144  // Build-in template function: highlight
   145  func (view *View) funcHighlight(key string, color string, str interface{}) string {
   146  	return gstr.Replace(gconv.String(str), key, fmt.Sprintf(`<span style="color:%s;">%s</span>`, color, key))
   147  }
   148  
   149  // Build-in template function: hidestr
   150  func (view *View) funcHideStr(percent int, hide string, str interface{}) string {
   151  	return gstr.HideStr(gconv.String(str), percent, hide)
   152  }
   153  
   154  // Build-in template function: toupper
   155  func (view *View) funcToUpper(str interface{}) string {
   156  	return gstr.ToUpper(gconv.String(str))
   157  }
   158  
   159  // Build-in template function: toupper
   160  func (view *View) funcToLower(str interface{}) string {
   161  	return gstr.ToLower(gconv.String(str))
   162  }
   163  
   164  // Build-in template function: nl2br
   165  func (view *View) funcNl2Br(str interface{}) string {
   166  	return gstr.Nl2Br(gconv.String(str))
   167  }