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 communication. To present numbers, texts, and other content in a more professional and clear manner, AnQi CMS providesstringformatA filter, it is like a multifunctional formatting tool that can help us finely control the display details of content.

stringformatA filter plays a role similar to Go language in AnQi CMS.fmt.Sprintf()The role of a function, which means it has rich formatting options, far more than simple string replacement.By using it, we can directly perform complex formatting operations on variables in the template, such as controlling the decimal precision of numbers, adjusting the alignment of text, and applying specific display rules based on different data types.

Precision control of the decimal point accuracy of numbers

It is crucial to control the number of decimal places in scenarios where prices, percentages, or any precise numerical values are displayed.stringformatThe filter provides flexible numeric precision control, ensuring your data is both accurate and beautiful.

For example, when we have a floating-point number3.141592653In order to display only two decimal places on the page, you can use it like this:

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

here,%.2fof.2Specified to retain two decimal places,fRepresents formatting a number as a floating-point number. If you need to keep one decimal place, just.2changed to.1The system will round it automatically. For example,123.4567Formatted as%.1fwill be displayed123.5This precise control ensures that financial data, scientific data, and so on are presented in a standardized manner, avoiding visual confusion caused by redundant decimals.

Flexible text alignment and filling settings

In addition to numerical precision, the alignment of content and whitespace padding also significantly affects the overall layout of the page, especially when constructing tables or columnar layouts.stringformatThe filter can help us easily align text to the left and right and fill with fixed width.

By default, when you specify a width for a string or number,stringformatit will align contentto the rightAlign and fill spaces on the left to reach the specified total width. For example, if we want a string to be right-aligned within 15 characters:

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

Here%15sIt indicates that the string will besFormatted to a width of 15 characters. If the original string is not long enough, the blank part will be filled with spaces on the left.

If you want to contentLeft-alignedAnd fill in the space 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, as it ensures good visual consistency 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 strength lies in its support for Go languagefmt.SprintfRich formatting verbs, which means it can handle various data types and output in the way you need.

Some common formatting verbs include:

  • %d: Formatted as a decimal integer.
  • %s: Formatted as a basic string.
  • %t: Formatted as a boolean value (true/false).
  • %xor%XFormat as lowercase or uppercase hexadecimal.
  • %for%e/%EFormat as a floating-point number or in scientific notation.
  • %vFormat 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 in a description:

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

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

Practical scenario: Create a professional product display

Imagine you are building a product list page that needs to display the product name, price, and inventory. In order to present this information in a uniform and professional manner, we can use a combination ofstringformatThe various functions:

{% 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 width is consistent):

产品名称:高性能服务器       
价格: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 length is different, it can maintain visual neatness and consistency, greatly enhancing the user's reading experience.

In conclusion, in AnQi CMSstringformatThe filter is a powerful and highly flexible tool. It helps us go beyond simple content display, through numerical precision control, alignment adjustment, and varied data type formatting, transforming raw data into beautifully typeset, clear information website content.Master and make good use of these advanced formatting options, no doubt, it will enable your security CMS website to rise to a higher level in visual presentation and information delivery, bringing users a better browsing experience.


Frequently Asked Questions (FAQ)

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

floatformatThe filter is mainly used for the formatting of floating-point numbers, its functions are relatively concentrated, mainly solving the problem of retaining decimal places of floating-point numbers (including rounding) and displaying zero values. AndstringformatFilter 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 accuracy, text alignment, padding characters, etc. If your requirement is limited to the decimal places of floating-point numbers,floatformatMay be more concise; but if necessary