How to format any values such as numbers, strings, arrays, etc. into strings and output in the Anqi CMS template?
stringformat
The filter can format any value such as numbers, strings, arrays, etc. into strings and output them in a specified format.
Some formatting standards:%v
Output structure {10 30}%+v
Output structure display field name {one:10 tow:30}%#v
Output structure source code snippet main.Point{one:10, tow:30}%T
Type of output value main.Point%t
Output formatted boolean true%d
Output standard decimal format 100%b
Output standard binary format 99 corresponding to 1100011%c
Output the corresponding characters of a fixed integer 99 corresponding c%x
Output hexadecimal code 99 corresponding to 63%f
Output decimal formatting 99 corresponding to 63%e
Output scientific scientific notation representation form 123400000.0 corresponding to 1.234000e+08%E
Output scientific scientific notation representation form 123400000.0 corresponding to 1.234000e+08%s
Perform basic string output ""string"" corresponding to "string"%q
The output with double quotes in the source code ""string"" corresponds to ""string""%p
Output 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.2f
Specify the output width of the floating point type 1.2 corresponding to 1.20%*2.2f
Specify the output width alignment of the floating point type, using-
Flag 1.2 corresponds to *1.20
How to use
stringformat
How to use filters:
{{ obj|stringformat:"Format definition" }}
For example3.141592653
Formatted to a number that retains 2 decimal places, you can write this:
{{ 3.141592653|stringformat:"%.2f" }} # Show result 3.14
Sample Demo
{{ simple.float|stringformat:"%.2f" }} {{ simple.uint|stringformat:"Test: %d" }} {{ simple.chinese_hello_world|stringformat:"Chinese: %s" }} # Show results Hello! 3.14 Test: 8 Chinese: Hello World