In content operations, the way content is presented often determines the first impression and reading experience of users.Sometimes, simple variable output cannot meet our fine needs for data formatting display, such as hoping to retain a fixed number of decimal places for numbers, or adding specific text before output.stringformatThe filter is the key tool for achieving refined output. It is like thefmt.Sprintf()Function, which can customize the format of any variable for output, greatly enhancing the flexibility of the template.
Core Features and Usage
stringformatThe core of the filter lies in its 'formatting' ability.When a variable in the template (whether it is a number, string, boolean, or even a complex data structure) needs to be displayed with a specific layout or content, it can be used.For example, you may need to always display the product price with two decimal places, or add a fixed prefix and pad the number of digits before the order number, or want to view the detailed type or structure of a variable in debug mode.stringformatThis is designed for such scenarios, it allows us to precisely control the final display format of variables through a formatted string.
Basic syntax
UsestringformatThe filter is very intuitive, its basic form is like this:
{{ 变量 | stringformat:"格式定义" }}
Here,变量is any template variable you want to format格式定义It is a string containing placeholders that tellsstringformatHow to handle and output变量the value. This格式定义string syntax in Go language isfmt.Sprintf()very similar, mastering it will unlock powerful formatting capabilities.
common formatting placeholders
格式定义The string contains various placeholders, which provide different formatting methods for different types of data. Here are some of the most commonly used placeholders and their purposes:
Numeric placeholder:
%d: Formatted as a decimal integer.%f: Formatted as a decimal floating-point number.%e/%E: Formatted as a floating-point number in scientific notation.%b: Formatted as a binary number.%x/%X: Format as a hexadecimal number.%o: Format as an octal number.
String type placeholder:
%s: Format as a basic string.%q: Formatted as a string with double quotes, special characters will be escaped.
Universal and type information placeholder:
%v: Default format representation of the variable.%+v: When the variable is a struct, output its field names and field values.%#v: When the variable is a struct, output its representation in Go language source code form.%T: 输出变量的English类型。%t: 格式化Boolean值(true或false)。
Width and precision control:You can also add numbers in placeholders to control the width and precision of the output.
%.2f: Format the floating-point number to two decimal places.%5d: Format integer, total width is 5, pad with spaces on the left if not enough.%-10s: Format string, total width is 10, left-aligned, pad with spaces on the right if not enough.%05d: Format integer, total width is 5, and pad with zero on the left if it's not enough.
Practical Case
To better understandstringformatusability, let's look at several specific application scenarios:
1. English control the decimal places of floating-point numbers precisely.
Suppose you have a price variable.pricewith a value of199.998You want it to always display as two decimal places.
{% set price = 199.998 %}
<p>商品价格:{{ price|stringformat:"%.2f" }} 元</p>
{# 输出:商品价格:199.99 元 #}
2. Add a prefix to numbers and zero fill.
If your order number isorderIdan integer, you want it to be displayed in the form ofORD-00123format.
{% set orderId = 123 %}
<p>订单编号:{{ orderId|stringformat:"ORD-%05d" }}</p>
{# 输出:订单编号:ORD-00123 #}
Here are the%05dindicating that the integer is formatted to a total width of 5 characters, with insufficient parts filled with zeros on the left.
3. Format output string
You may need to add a specific display text for the website name, or output it in the form of a Go language string literal.
{% set siteName = "安企CMS" %}
<p>欢迎来到:{{ siteName|stringformat:"%s 官方网站" }}</p>
{# 输出:欢迎来到:安企CMS 官方网站 #}
<p>编程语言:{{ "Go语言"|stringformat:"%q" }}</p>
{# 输出:编程语言:"Go语言" #}
4. Check variable type or debug output
It is very useful to understand the actual type or internal structure of variables when developing or debugging templates.
myVar的类型:{{ myVar|stringformat:“%T” }}
{# 输出:myVar的类型:English #}myNum的详细信息:{{ myNum|stringformat:“%v” }}
{# 输出:myNum的详细信息:English #}myData's Go language structure: {{ myData|stringformat:“%#v” }}
{# 输出类似:myData的Go语言结构:map[string]interface