In website operations, we often need to handle various numerical strings with specific formats, such as order numbers, product codes, or member IDs.These numbers usually need to maintain a certain length, and leading zeros are used to fill in when the numbers are insufficient to ensure uniformity and aesthetics.AnQiCMS (AnQiCMS) provides a very practical template filter——stringformatCan help us easily meet these needs.
UnderstandingstringformatThe role of the filter
stringformatThe filter plays a role in data formatting in Anqi CMS templates. Its working principle is similar to that in Go language.fmt.Sprintf()The function is very similar, able to convert different types of data (such as numbers, strings) into the final string format as specified.This means you can precisely control the way numbers are displayed, such as fixed width, leading zero padding, etc.
stringformatusage
UsestringformatThe basic syntax of the filter is very intuitive:
{{ obj|stringformat:"格式定义" }}
here,objThis is the variable or value you want to format, and"格式定义"It is a string that contains the rules you want the data to present.
The key to mastering number formatting: leading zeros and specific width
For generating numbers like order numbers that have leading zeros or specific widths, we need to understand some commonly used format specifiers:
- Integer type (
d):dIs used as a type identifier for formatting decimal integers. - Minimum width (
x):In%anddInsert a number betweenx, indicating the minimum width of the output string. If the length of the number itself is less thanxIf it will be filled. - Leading zero padding (
0):If the minimum width digitxIs added in front of0For example%0xd, then when the number length is insufficientxWhen, the system will use leading zeros instead of spaces for padding.
Example: Generate numbers with leading zeros.
Suppose we want a number to always be displayed as 5 digits, with leading zeros if it is less than 5 digits:
{{ 123|stringformat:"%05d" }}
The output of the above code will be:00123.
Example: Generate a number with a specific width and fill with spaces
If we don't add0it will be filled with spaces by default:
{{ 123|stringformat:"%5d" }}
The output of the above code will be:123(There are two spaces in front).
In the actual application of the Anqi CMS template
Imagine you are building an order detail page for a website, and you need to display a standardized order number, or display a product ID with a fixed number of digits in a product list. At this point,stringformatit comes in handy.
Scenario one: Format the document ID as an order number or product code
In Anqi CMS, documents (archive) Typically, it has a unique ID. We can use this ID to generate numbers with prefixes and leading zeros.
For example, get the ID of a document, format it as a 6-digit number with leading zeros, and add the prefix "ORD-":
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<div>
<!-- 假设 item.Id 是一个订单ID,比如 1 -->
订单号:ORD-{{ item.Id|stringformat:"%06d" }}
</div>
{% endfor %}
{% endarchiveList %}
Ifitem.IdIs1The output will be:订单号:ORD-000001Ifitem.IdIs12345The output will be:订单号:ORD-012345.
This ensures that regardless of the length of the ID, the final order number maintains a uniform length and format.
Scenario two: Format custom fields
If you define a custom numeric field in the content model, such as "Product Batch Number", you can also format it:
{% archiveDetail product_info with name="BatchNumber" %}
<!-- 假设 BatchNumber 是一个数字,比如 789 -->
产品批次号:BATCH-{{ product_info|stringformat:"%04d" }}
{% endarchiveDetail %}
IfBatchNumberIs789The output will be:产品批次号:BATCH-0789.
Combined with other template tags and variables
stringformatThe filter can be flexibly combined with other safe CMS template tags and filters.You can format the ID of each item in the loop and also assign the formatted result to a new variable for reuse in other parts of the template.
{% set raw_id = item.Id %}
{% set formatted_order_id = raw_id|stringformat:"%06d" %}
<div>
格式化后的订单ID:ORD-{{ formatted_order_id }}
</div>
Summary
BystringformatThe filter, Anqi CMS provides a powerful and flexible tool for content operators and template developers, allowing precise control over the display format of numbers on the page. Whether it's creating a unified order number, product code, or any other numeric string that requires a fixed length and leading zero padding,stringformatCan help you a hand, making the website content more professional and standardized.
Frequently Asked Questions (FAQ)
Q1:stringformatandaddWhat are the differences between filters?A1:stringformatThe filter is mainly used to convert data (such as numbers or strings) into a string output according to predefined format rules, focusing on format control, such as zero-padding, fixed width, decimal places, etc. AndaddThe filter is used to add two numbers together or concatenate two strings, it focuses on numerical calculations or string concatenation operations.
Q2: If I want to format a variable that is not a numeric type but uses%dsuch a numeric format character, what will happen?A2:stringformatThe filter tries to perform type conversion internally. If your variable is a string representation of a number (such as '123'), it usually converts it to a number correctly and according to%dFormat. But if the variable is a completely non-numeric string (for example, "hello"), then use%dThis could result in output of0Or an empty string, the specific behavior depends on the internal implementation. To ensure accuracy, it is best to confirm the type of the variable or use a more general%s(string) format symbol.
Q3: How to add a custom prefix to a formatted number string, such as 'ORD-' or 'SKU-'?A3: In the Anqi CMS template, you can directly instringformatConcatenate the output result of the filter externally. For example, if you want to add the prefix "ORD-"
to a formatted number, you can write it like this:ORD-{{ item.Id|stringformat:"%06d" }}The filter will first complete the formatting of the number, and then the result will be connected with the 'ORD-' string.