In content operation, 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 that numbers retain fixed decimal places or that specific text is added before output.An enterprise CMS provides powerful content rendering capabilities for template developers and content operators, among whichstringformatThe filter is the key tool for achieving fine-grained output. It is like in the Go language,fmt.Sprintf()A function that can format the output of any variable to a custom format, greatly enhancing the flexibility of the template.
Core Function and Purpose
stringformatThe core of the filter lies in its "formatting" ability. It comes into play when the variables in the template (whether they are numbers, strings, boolean values, or even complex data structures) need to be displayed in a specific layout or content.For example, you may need to always display the product price with two decimal places, or add a fixed prefix and pad the order number, or want to view the detailed type or structure of a variable in debug mode.stringformatIt is born for these scenarios, it allows us to control the final display form of variables through a formatted string.
Basic syntax
UsestringformatThe filter is very intuitive, its basic form is like this:
{{ 变量 | stringformat:"格式定义" }}
Here, 变量This is any template variable you wish to format格式定义It is a placeholder string that tellsstringformathow to process and output变量the value. This格式定义The syntax of strings in Go language isfmt.Sprintf()very similar, mastering it will unlock powerful formatting capabilities.
Common formatting placeholders
格式定义Strings contain various placeholders that provide different formatting options for different types of data. The following are some of the most commonly used placeholders and their purposes:
Numeric type placeholders:
%d: Formatted as a decimal integer.%fFormatted as a floating-point number (decimal).%e/%EFormatted as a floating-point number in scientific notation.%bFormatted as a binary number.%x/%XFormatted as a hexadecimal number.%oFormatted as an octal number.
Placeholder for a string type.
%sFormatted as a basic string.%qFormatted as a quoted string, with special characters escaped.
General type information placeholder:
%v: The default format representation of a variable.%+v: When a variable is a struct, output its field names and field values.%#v: When the variable is a struct, output its Go language source code representation.%T: Output the Go language type of the variable.%t: Format boolean values (true or false).
Width and precision control:You can also add numbers in placeholders to control the width and precision of the output.
%.2f: Format floating-point number, retaining two decimal places.%5d: Format integer, total width of 5, fill with spaces on the left if insufficient.%-10s: Format a string, total width is 10, left-aligned, and the insufficient part is filled with spaces on the right.%05d: Format an integer, total width is 5, and the insufficient part is filled with zeros on the left.
Case Study
In order to understand betterstringformatThe practicality, let's look at some specific application scenarios:
1. Precisely control the decimal places of floating-point numbers
Suppose you have a price variablepricewith 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 them
If your order numberorderIdis an integer, you want it to be displayed in the form ofORD-00123format.
{% set orderId = 123 %}
<p>订单编号:{{ orderId|stringformat:"ORD-%05d" }}</p>
{# 输出:订单编号:ORD-00123 #}
Here%05dRepresents formatting an integer to a total width of 5 digits, padding with zeros on the left if necessary.
3. Format the 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.“`twig {% set myVar = “Hello AnQiCMS” %} {% set myNum = 123 %} {% set myData = {name:“AnQi”, version:“3.0”} %} {# 假设这是一个Map或结构体 #}
The type of myVar: {{ myVar|stringformat:%T }}
{# Output: The type of myVar: string #}The details of myNum: {{ myNum|stringformat:%v }}
{# Output: myNum's details: 123 #}myData's Go language structure: {{ myData|stringformat:%#v }}
{# Output similar: myData's Go language structure: map[string]interface