Format any value into string output

How to format numbers, strings, arrays, and any other values into a string with a specified format in the Anqi CMS template?

stringformatThe filter can format any value such as numbers, strings, arrays, etc. into strings and output them in a specified format.

Some formatting standards:

  • %vOutput structure {10 30}
  • %+vOutput structure display field name {one:10 tow:30}
  • %#vOutput structure source code snippet main.Point{one:10, tow:30}
  • %TType of output value main.Point
  • %tOutput formatted boolean true
  • %dOutput standard decimal format 100
  • %bOutput standard binary format 99 corresponding to 1100011
  • %cOutput the corresponding characters of a fixed integer 99 corresponding c
  • %xOutput hexadecimal code 99 corresponding to 63
  • %fOutput decimal formatting 99 corresponding to 63
  • %eOutput scientific scientific notation representation form 123400000.0 corresponding to 1.234000e+08
  • %EOutput scientific scientific notation representation form 123400000.0 corresponding to 1.234000e+08
  • %sPerform basic string output “\“string\”” corresponds to “string”}]
  • %qThe output with double quotes in the source code “\“string\”” corresponds to “\“string\””
  • %pOutput the value of a pointer &jgt corresponding to 0xc00004a090
  • %The output width is controlled later. The default result is right-aligned and the blank part is filled with spaces.
  • %2.2fSpecify the output width of the floating point type 1.2 corresponding to 1.20
  • %*2.2fSpecify the output width alignment of the floating point type, using-Flag 1.2 corresponds to *1.20

How to use

stringformatHow to use filters:

{{ obj|stringformat:"格式定义" }}

For example3.141592653Format the number to keep 2 decimal places, it can be written like this:

{{ 3.141592653|stringformat:"%.2f" }}
# 显示结果
3.14

Sample Demo

{{ simple.float|stringformat:"%.2f" }}
{{ simple.uint|stringformat:"Test: %d" }}
{{ simple.chinese_hello_world|stringformat:"Chinese: %s" }}
# 显示结果
Hello!
3.14
Test: 8
Chinese: 你好世界