plus/minus/times/divide
Function | Description | Format | Example | Remarks |
---|---|---|---|---|
plus | Add | {{.value1 | plus .value2}} | {{2 | plus 3}} => 5 | 3+2 |
minus | Subtract | {{.value1 | minus .value2}} | {{2 | minus 3}} => 1 | 3-2 |
times | Multiply | {{.value1 | times .value2}} | {{2 | times 3}} => 6 | 3*2 |
divide | Divide | {{.value1 | divide .value2}} | {{2 | divide 3}} => 1.5 | 3/2 |
text
{{.value | text}}
Removes HTML tags from the value
variable and displays only the text content (and removes script
tags). Example:
{{"<div>Test</div>"|text}}
// Output: Test
htmlencode/encode/html
{{.value | htmlencode}}
{{.value | encode}}
{{.value | html}}
Escapes the value
variable with HTML encoding. Example:
{{"<div>Test</div>"|html}}
// Output: <div>Test</div>
htmldecode/decode
{{.value | htmldecode}}
{{.value | decode}}
Decodes the HTML encoded value
variable. Example:
{{"<div>Test</div>" | htmldecode}}
// Output: <div>Test</div>
urlencode/url
{{.url | url}}
Escapes the url
variable in URL
encoding. Example:
{{"https://goframe.org" | url}}
// Output: https%3A%2F%2Fgoframe.org
urldecode
{{.url | urldecode}}
Decodes the URL
encoded url
variable. Example:
{{"https%3A%2F%2Fgoframe.org"|urldecode}}
// Output: https://goframe.org
date
{{.timestamp | date .format}}
{{date .format .timestamp}}
{{date .format}}
Formats the timestamp
variable into a date and time, similar to PHP's date
method. The format
parameter supports PHP date format. Additionally, refer to Time.
When timestamp
is null
(or 0
), the current time is used as the timestamp parameter.
Example:
{{1540822968 | date "Y-m-d"}}
{{"1540822968" | date "Y-m-d H:i:s"}}
{{date "Y-m-d H:i:s"}}
// Output:
// 2018-10-29
// 2018-10-29 22:22:48
// 2018-12-05 10:22:00
compare
{{compare .str1 .str2}}
{{.str2 | compare .str1}}
Compares the strings str1
and str2
. Return values: - 0 : str1
== str2
- 1 : str1
> str2
- -1 : str1
< str2
Example:
{{compare "A" "B"}}
{{compare "1" "2"}}
{{compare 2 1}}
{{compare 1 1}}
// Output:
// -1
// -1
// 1
// 0
replace
{{.str | replace .search .replace}}
{{replace .search .replace .str}}
Replaces search
with replace
in str
. Example:
{{"I'm中国人" | replace "I'm" "我是"}}
// Output:
// 我是中国人
substr
{{.str | substr .start .length}}
{{substr .start .length .str}}
Extracts a substring from str
starting at index start
(index starts from 0) for length
characters, supporting Chinese characters, similar to PHP's substr
function. Example:
{{"我是中国人" | substr 2 -1}}
{{"我是中国人" | substr 2 2}}
// Output:
// 中国人
// 中国
strlimit
{{.str | strlimit .length .suffix}}
Limits the str
string to length
characters, supporting Chinese characters, and appends the suffix
string if it exceeds the length. Example:
{{"我是中国人" | strlimit 2 "..."}}
// Output:
// 我是...
concat
{{concat .str1 .str2 .str3...}}
Concatenates strings. Example:
{{concat "我" "是" "中" "国" "人"}}
// Output:
// 我是中国人
hidestr
{{.str | hidestr .percent .hide}}
Hides characters in str
according to a percent
percentage, hiding towards the center of the string using the hide
variable, commonly used for names, phone numbers, email addresses, and ID numbers. It supports Chinese characters and email formats. Example:
{{"热爱GF热爱生活" | hidestr 20 "*"}}
{{"热爱GF热爱生活" | hidestr 50 "*"}}
// Output:
// 热爱GF*爱生活
// 热爱****生活
highlight
{{.str | highlight .key .color}}
Highlights the keyword key
in str
with the specified color color
. Example:
{{"热爱GF热爱生活" | highlight "GF" "red"}}
// Output:
// 热爱<span style="color:red;">GF</span>热爱生活
toupper/tolower
{{.str | toupper}}
{{.str | tolower}}
Converts the case of str
. Example:
{{"gf" | toupper}}
{{"GF" | tolower}}
// Output:
// GF
// gf
nl2br
{{.str | nl2br}}
Replaces \n/\r
in str
with HTML <br />
tags. Example:
{{"Go\nFrame" | nl2br}}
// Output:
// Go<br />Frame
dump
{{dump .var}}
Formats and prints the variable, similar to the g.Dump
function, often used for development and debugging. Example:
gview.Assign("var", g.Map{
"name" : "john",
})
{{dump .var}}
// Output:
// <!--
// {
// name: "john"
// }
// -->
map
{{map .var}}
Converts a template variable to map[string]interface{}
type, commonly used for range...end
iteration.
maps
{{maps .var}}
Converts a template variable to []map[string]interface{}
type, commonly used for range...end
iteration.
json/xml/ini/yaml/yamli/toml
Function | Description | Format |
---|---|---|
json | Converts a template variable to a JSON formatted string. | {{json .var}} |
xml | Converts a template variable to an XML formatted string. | {{xml .var}} |
ini | Converts a template variable to an INI formatted string. | {{ini .var}} |
yaml | Converts a template variable to a YAML formatted string. | {{yaml .var}} |
yamli | Converts a template variable to a YAML formatted string with custom indentation. | {{yamli .var .indent}} |
toml | Converts a template variable to a TOML formatted string. | {{toml .var}} |