In the powerful template system of AnQi CMS, we often need to present dynamic data on the website, and the way the data is displayed directly affects user experience and the efficiency of information conveyance. In order to present numbers, text and other content in a more professional and clearer way, AnQi CMS providesstringformatFilter, it is like a multifunctional formatting tool that can help us finely control the display details of the content.

stringformatFilter plays a role similar to that of Go language in the Anqi CMS.fmt.Sprintf()

Control the decimal precision of numbers precisely.

Controlling the number of decimal places is crucial in displaying prices, percentages, or any scenario that requires precise numbers.stringformatThe filter provides flexible numeric precision control, ensuring that your data is both accurate and aesthetically pleasing.

For example, when we have a floating-point number3.141592653The number is displayed with only two decimal places on the page, and it can be used like this:

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

Here,%.2fof.2specifies two decimal places,fRepresents formatting the number as a floating-point number. If you need to retain one decimal place, just.2to.1The system will round off automatically. For example,123.4567格式化为English%.1fThen it will display123.5This precise control ensures that financial data, scientific data, etc., are presented in a standardized manner, avoiding visual confusion caused by redundant decimals.

Flexible text alignment and filling settings

The alignment of content and whitespace padding also has a significant impact on the overall layout of the page, especially when building tables or columnar layouts.stringformatFilter can help us easily achieve left and right alignment of text as well as fixed-width padding.

By default, when you specify a width for a string or number,stringformatit will adjust the contentright-alignedEnglish, and fill with spaces on the left until the specified total width is reached. For example, we want to right-align a string within a width of 15 characters:

{{ "AnQiCMS"|stringformat:"%15s" }}
{# 显示结果:        AnQiCMS (AnQiCMS前面有8个空格) #}

Here are the%15smeans to represent the stringsFormat to 15 character width. If the original string length is insufficient, the blank portion will be filled with spaces on the left.

If you wish to display the contentLeft aligned,and fill in the spaces on the right, we can add a negative sign before the width,-):

{{ "AnQiCMS"|stringformat:"%-15s" }}
{# 显示结果:AnQiCMS        (AnQiCMS后面有8个空格) #}

This alignment method is very practical when displaying list items, product names, or any text that needs to be neatly arranged. It ensures good visual consistency of the content within a fixed area.

These alignment and padding options can also be combined with number formatting. For example, a floating-point number needs to be left-aligned and retain two decimal places:

{{ 123.456|stringformat:"%-10.2f" }}
{# 显示结果:123.46    (数字后有4个空格) #}

Formatting of diverse data types

stringformatThe power of it also lies in its support for Go languagefmt.SprintfRich in formatting verbs, which means it can handle a variety of data types and output them in the way you need.

Some commonly used formatting verbs include:

  • %d: Formatted as a decimal integer.
  • %s: Format as a basic string.
  • %t: Format as a boolean value (true/false).
  • %xor%X: Format as lowercase or uppercase hexadecimal.
  • %for%e/%E: Format as a floating-point number or scientific notation.
  • %v: Format as the default representation of the value, suitable for any type.

For example, convert an integer to hexadecimal:

{{ 255|stringformat:"%x" }}
{# 显示结果:ff #}

Or embed a boolean value into a description:

{{ true|stringformat:"当前状态: %t" }}
{# 显示结果:当前状态: true #}

These flexible options allow you to choose the most suitable formatting method according to the actual data content and display requirements, thereby avoiding manual concatenation or complex conditional judgments, greatly simplifying the template code.

实战场景:创建专业的产品展示

Imagine you are building a product list page, which needs to display the product name, price, and stock. In order to present this information in an organized and professional manner, we can use a comprehensive approach.stringformatThe various functions of:

{% set productName = "高性能服务器" %}
{% set productPrice = 1999.99 %}
{% set productStock = 75 %}

<p>产品名称:{{ productName|stringformat:"%-20s" }}</p>
<p>价格:{{ productPrice|stringformat:"%-10.2f" }} 元</p>
<p>库存:{{ productStock|stringformat:"%5d" }} 件</p>

This code will display the following effect (assuming each character has the same width):

产品名称:高性能服务器       
价格:1999.99     元
库存:   75 件

By uniformly setting the width of strings, the precision of floating-point numbers, and the alignment of integers, even if the data lengths are different, a visually tidy and consistent appearance can be maintained, greatly enhancing the user reading experience.

In summary, in the Anqi CMS,stringformat


Common Questions (FAQ)

1.stringformatandfloatformatWhat are the differences between filters? Which one should I choose?

floatformatThe filter is mainly used for the formatting of floating-point numbers, its functions are relatively centralized, mainly to solve the problem of retaining decimal places of floating-point numbers (including rounding) and displaying zero values.stringformatFilter is a more general formatting tool, which not only supports floating-point numbers but also handles integers, strings, boolean values and other data types, and provides more rich formatting options such as number precision, text alignment, fill characters, etc. If your needs are limited to the decimal places of floating-point numbers, floatformatMay be more concise; but if necessary