Template Funcs - Intro - 图1tip

Below are some basic syntax and basic functions from the Golang standard library. The GoFrame framework has made necessary improvements to some basic functions.

Variables can be passed between functions using the | symbol

  1. {{.value | Func1 | Func2}}

Use parentheses

  1. {{printf "nums is %s %d" (printf "%d %d" 1 2) 3}}

and

  1. {{and .X .Y .Z}}

and will evaluate each argument and return the first empty argument, otherwise, it returns the last non-empty argument.

call

  1. {{call .Field.Func .Arg1 .Arg2}}

call can invoke a function and pass in parameters.

The called function needs to return 1 or 2 values. When returning two values, the second value is used to return an error of type error. Execution will terminate if the returned error is not nil.

index

index supports map, slice, array, string, and reads the value of the specified index for these types.

  1. {{index .Maps "name"}}

len

  1. {{printf "The content length is %d" (.Content|len)}}

Returns the length of the corresponding type, supporting types: map, slice, array, string, chan.

not

not returns the negation of the input argument.

For example, to determine if a variable is empty:

  1. {{if not .Var}}
  2. // Executes when empty (.Var is empty, like: nil, 0, "", slice/map of length 0)
  3. {{else}}
  4. // Executes when not empty (.Var is not empty)
  5. {{end}}

or

  1. {{or .X .Y .Z}}

or will evaluate each argument and return the first non-empty argument, otherwise, it returns the last argument.

print

Same as fmt.Sprint.

printf

Same as fmt.Sprintf.

println

Same as fmt.Sprintln.

urlquery

  1. {{urlquery "http://johng.cn"}}

Will return

  1. http%3A%2F%2Fjohng.cn

eq / ne / lt / le / gt / ge

These functions are generally used within an if statement

  1. `eq`: arg1 == arg2
  2. `ne`: arg1 != arg2
  3. `lt`: arg1 < arg2
  4. `le`: arg1 <= arg2
  5. `gt`: arg1 > arg2
  6. `ge`: arg1 >= arg2

The eq function is different from the others in that it supports multiple arguments.

  1. {{eq arg1 arg2 arg3 arg4}}

Is the same as the following logical evaluation:

  1. arg1==arg2 || arg1==arg3 || arg1==arg4 ...

Used with if

  1. {{if eq true .Var1 .Var2 .Var3}}...{{end}}
  1. {{if lt 100 200}}...{{end}}

For example, executing when the variable is not empty:

  1. {{if .Var}}
  2. // Executes when not empty (.Var is not empty)
  3. {{else}}
  4. // Executes when empty (.Var is empty, like: nil, 0, "", slice/map of length 0)
  5. {{end}}

Comparison Function Improvements

The GoFrame framework’s template engine has made necessary improvements to the standard library’s comparison template functions eq/ne/lt/le/gt/ge, to support comparison of any data type. For instance, the following comparison in the standard library template:

  1. {{eq 1 "1"}}

Will cause an error:

  1. panic: template: at <eq 1 "1">: error calling eq: incompatible types for comparison

Because the two parameters being compared are not of the same data type, a panic error is triggered.

In the GoFrame framework’s template engine, the two parameters will be automatically converted to the same data type before comparison, providing a better development experience and greater flexibility. If both parameters are integer variables (or integer strings), they will be converted to integers for comparison; otherwise, they will be converted to string variables for comparison (case-sensitive).

Improved Execution Example

Let’s look at an example of running comparison template functions in the GoFrame framework’s template engine.

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. tplContent := `
  9. eq:
  10. eq "a" "a": {{eq "a" "a"}}
  11. eq "1" "1": {{eq "1" "1"}}
  12. eq 1 "1": {{eq 1 "1"}}
  13. ne:
  14. ne 1 "1": {{ne 1 "1"}}
  15. ne "a" "a": {{ne "a" "a"}}
  16. ne "a" "b": {{ne "a" "b"}}
  17. lt:
  18. lt 1 "2": {{lt 1 "2"}}
  19. lt 2 2 : {{lt 2 2 }}
  20. lt "a" "b": {{lt "a" "b"}}
  21. le:
  22. le 1 "2": {{le 1 "2"}}
  23. le 2 1 : {{le 2 1 }}
  24. le "a" "a": {{le "a" "a"}}
  25. gt:
  26. gt 1 "2": {{gt 1 "2"}}
  27. gt 2 1 : {{gt 2 1 }}
  28. gt "a" "a": {{gt "a" "a"}}
  29. ge:
  30. ge 1 "2": {{ge 1 "2"}}
  31. ge 2 1 : {{ge 2 1 }}
  32. ge "a" "a": {{ge "a" "a"}}
  33. `
  34. content, err := g.View().ParseContent(context.TODO(), tplContent, nil)
  35. if err != nil {
  36. panic(err)
  37. }
  38. fmt.Println(content)
  39. }

After running, the output result is:

  1. eq:
  2. eq "a" "a": true
  3. eq "1" "1": true
  4. eq 1 "1": true
  5. ne:
  6. ne 1 "1": false
  7. ne "a" "a": false
  8. ne "a" "b": true
  9. lt:
  10. lt 1 "2": true
  11. lt 2 2 : false
  12. lt "a" "b": true
  13. le:
  14. le 1 "2": true
  15. le 2 1 : false
  16. le "a" "a": true
  17. gt:
  18. gt 1 "2": false
  19. gt 2 1 : true
  20. gt "a" "a": false
  21. ge:
  22. ge 1 "2": false
  23. ge 2 1 : true
  24. ge "a" "a": true