Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update helper.go
  • Loading branch information
kerwin612 authored Jun 11, 2024
commit 1344d99fd8c1c73f7c68b51a201055305ae32c9c
25 changes: 24 additions & 1 deletion modules/templates/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"html"
"html/template"
"net/url"
"reflect"
"slices"
"strings"
"time"
Expand Down Expand Up @@ -238,14 +239,36 @@ func DotEscape(raw string) string {
// Iif is an "inline-if", similar util.Iif[T] but templates need the non-generic version,
// and it could be simply used as "{{Iif expr trueVal}}" (omit the falseVal).
func Iif(condition any, vals ...any) any {
if condition != nil && condition.(bool) {
if IsTruthy(condition) {
return vals[0]
} else if len(vals) > 1 {
return vals[1]
}
return nil
}

func IsTruthy(v any) bool {
if v == nil {
return false
}

rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Bool:
return rv.Bool()
case reflect.String:
return rv.String() != ""
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
return rv.Float() > 0 || rv.Int() > 0
case reflect.Slice, reflect.Array, reflect.Map, reflect.Ptr, reflect.Interface:
return !rv.IsNil() && rv.Len() > 0
default:
return false
}
}

// Eval the expression and return the result, see the comment of eval.Expr for details.
// To use this helper function in templates, pass each token as a separate parameter.
//
Expand Down