Skip to main content
Version: 2.8.x(Latest)
tip

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

{{.value | Func1 | Func2}}

Use parentheses

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

and

{{and .X .Y .Z}}

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

call

{{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.

{{index .Maps "name"}}

len

{{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:

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

or

{{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

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

Will return

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

eq / ne / lt / le / gt / ge

These functions are generally used within an if statement

`eq`: arg1 == arg2
`ne`: arg1 != arg2
`lt`: arg1 < arg2
`le`: arg1 <= arg2
`gt`: arg1 > arg2
`ge`: arg1 >= arg2

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

{{eq arg1 arg2 arg3 arg4}}

Is the same as the following logical evaluation:

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

Used with if

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

For example, executing when the variable is not empty:

{{if .Var}}
// Executes when not empty (.Var is not empty)
{{else}}
// Executes when empty (.Var is empty, like: nil, 0, "", slice/map of length 0)
{{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:

{{eq 1 "1"}}

Will cause an error:

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.

package main

import (
"context"
"fmt"
"github.com/gogf/gf/v2/frame/g"
)

func main() {
tplContent := `
eq:
eq "a" "a": {{eq "a" "a"}}
eq "1" "1": {{eq "1" "1"}}
eq 1 "1": {{eq 1 "1"}}

ne:
ne 1 "1": {{ne 1 "1"}}
ne "a" "a": {{ne "a" "a"}}
ne "a" "b": {{ne "a" "b"}}

lt:
lt 1 "2": {{lt 1 "2"}}
lt 2 2 : {{lt 2 2 }}
lt "a" "b": {{lt "a" "b"}}

le:
le 1 "2": {{le 1 "2"}}
le 2 1 : {{le 2 1 }}
le "a" "a": {{le "a" "a"}}

gt:
gt 1 "2": {{gt 1 "2"}}
gt 2 1 : {{gt 2 1 }}
gt "a" "a": {{gt "a" "a"}}

ge:
ge 1 "2": {{ge 1 "2"}}
ge 2 1 : {{ge 2 1 }}
ge "a" "a": {{ge "a" "a"}}
`
content, err := g.View().ParseContent(context.TODO(), tplContent, nil)
if err != nil {
panic(err)
}
fmt.Println(content)
}

After running, the output result is:

eq:
eq "a" "a": true
eq "1" "1": true
eq 1 "1": true

ne:
ne 1 "1": false
ne "a" "a": false
ne "a" "b": true

lt:
lt 1 "2": true
lt 2 2 : false
lt "a" "b": true

le:
le 1 "2": true
le 2 1 : false
le "a" "a": true

gt:
gt 1 "2": false
gt 2 1 : true
gt "a" "a": false

ge:
ge 1 "2": false
ge 2 1 : true
ge "a" "a": true