plus/minus/times/divide

FunctionDescriptionFormatExampleRemarks
plusAdd{{.value1 | plus .value2}}{{2 | plus 3}} => 53+2
minusSubtract{{.value1 | minus .value2}}{{2 | minus 3}} => 13-2
timesMultiply{{.value1 | times .value2}}{{2 | times 3}} => 63*2
divideDivide{{.value1 | divide .value2}}{{2 | divide 3}} => 1.53/2

text

  1. {{.value | text}}

Removes HTML tags from the value variable and displays only the text content (and removes script tags). Example:

  1. {{"<div>Test</div>"|text}}
  2. // Output: Test

htmlencode/encode/html

  1. {{.value | htmlencode}}
  2. {{.value | encode}}
  3. {{.value | html}}

Escapes the value variable with HTML encoding. Example:

  1. {{"<div>Test</div>"|html}}
  2. // Output: &lt;div&gt;Test&lt;/div&gt;

htmldecode/decode

  1. {{.value | htmldecode}}
  2. {{.value | decode}}

Decodes the HTML encoded value variable. Example:

  1. {{"&lt;div&gt;Test&lt;/div&gt;" | htmldecode}}
  2. // Output: <div>Test</div>

urlencode/url

  1. {{.url | url}}

Escapes the url variable in URL encoding. Example:

  1. {{"https://goframe.org" | url}}
  2. // Output: https%3A%2F%2Fgoframe.org

urldecode

  1. {{.url | urldecode}}

Decodes the URL encoded url variable. Example:

  1. {{"https%3A%2F%2Fgoframe.org"|urldecode}}
  2. // Output: https://goframe.org

date

  1. {{.timestamp | date .format}}
  2. {{date .format .timestamp}}
  3. {{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:

  1. {{1540822968 | date "Y-m-d"}}
  2. {{"1540822968" | date "Y-m-d H:i:s"}}
  3. {{date "Y-m-d H:i:s"}}
  4. // Output:
  5. // 2018-10-29
  6. // 2018-10-29 22:22:48
  7. // 2018-12-05 10:22:00

compare

  1. {{compare .str1 .str2}}
  2. {{.str2 | compare .str1}}

Compares the strings str1 and str2. Return values: - 0 : str1 == str2 - 1 : str1 > str2 - -1 : str1 < str2

Example:

  1. {{compare "A" "B"}}
  2. {{compare "1" "2"}}
  3. {{compare 2 1}}
  4. {{compare 1 1}}
  5. // Output:
  6. // -1
  7. // -1
  8. // 1
  9. // 0

replace

  1. {{.str | replace .search .replace}}
  2. {{replace .search .replace .str}}

Replaces search with replace in str. Example:

  1. {{"I'm中国人" | replace "I'm" "我是"}}
  2. // Output:
  3. // 我是中国人

substr

  1. {{.str | substr .start .length}}
  2. {{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:

  1. {{"我是中国人" | substr 2 -1}}
  2. {{"我是中国人" | substr 2 2}}
  3. // Output:
  4. // 中国人
  5. // 中国

strlimit

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

  1. {{"我是中国人" | strlimit 2 "..."}}
  2. // Output:
  3. // 我是...

concat

  1. {{concat .str1 .str2 .str3...}}

Concatenates strings. Example:

  1. {{concat "我" "是" "中" "国" "人"}}
  2. // Output:
  3. // 我是中国人

hidestr

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

  1. {{"热爱GF热爱生活" | hidestr 20 "*"}}
  2. {{"热爱GF热爱生活" | hidestr 50 "*"}}
  3. // Output:
  4. // 热爱GF*爱生活
  5. // 热爱****生活

highlight

  1. {{.str | highlight .key .color}}

Highlights the keyword key in str with the specified color color. Example:

  1. {{"热爱GF热爱生活" | highlight "GF" "red"}}
  2. // Output:
  3. // 热爱<span style="color:red;">GF</span>热爱生活

toupper/tolower

  1. {{.str | toupper}}
  2. {{.str | tolower}}

Converts the case of str. Example:

  1. {{"gf" | toupper}}
  2. {{"GF" | tolower}}
  3. // Output:
  4. // GF
  5. // gf

nl2br

  1. {{.str | nl2br}}

Replaces \n/\r in str with HTML <br /> tags. Example:

  1. {{"Go\nFrame" | nl2br}}
  2. // Output:
  3. // Go<br />Frame

dump

  1. {{dump .var}}

Formats and prints the variable, similar to the g.Dump function, often used for development and debugging. Example:

  1. gview.Assign("var", g.Map{
  2. "name" : "john",
  3. })
  1. {{dump .var}}
  2. // Output:
  3. // <!--
  4. // {
  5. // name: "john"
  6. // }
  7. // -->

map

  1. {{map .var}}

Converts a template variable to map[string]interface{} type, commonly used for range...end iteration.

maps

  1. {{maps .var}}

Converts a template variable to []map[string]interface{} type, commonly used for range...end iteration.

json/xml/ini/yaml/yamli/toml

FunctionDescriptionFormat
jsonConverts a template variable to a JSON formatted string.{{json .var}}
xmlConverts a template variable to an XML formatted string.{{xml .var}}
iniConverts a template variable to an INI formatted string.{{ini .var}}
yamlConverts a template variable to a YAML formatted string.{{yaml .var}}
yamliConverts a template variable to a YAML formatted string with custom indentation.{{yamli .var .indent}}
tomlConverts a template variable to a TOML formatted string.{{toml .var}}