How to optimize the display format of text and numbers using AnQiCMS filters (such as truncatechars, floatformat)?

Calendar 👁️ 77

How to make information concise, clear, and professional in website content operation?This is often a challenge faced by many operators. Whether it is a news summary, product description, or price data, statistics, if displayed in a chaotic manner, it not only affects the user experience but may also lead to a deviation in information transmission.AnQiCMS (AnQiCMS) understands this pain point, its powerful template engine is built-in with a series of practical filters that can help us easily optimize the display format of text and numbers, making your website content look fresh.

Refine text, enhance readability

The articles, product descriptions, and other content on our website may need to be presented in different lengths in different display scenarios (such as list pages, detail pages, sidebars).Long text takes up too much space and affects the layout; short text may not provide enough information.The text filter of AnQiCMS is exactly the tool to solve these problems.

Intelligent text truncation:truncatecharswithtruncatewords

Assuming you have a long article abstract and you want to only display the beginning part on the list page, and automatically add an ellipsis. At this time,truncatecharsThe filter comes into play. It will truncate the text according to the specified number of characters and add it at the end.....

For example, if the description of an article isarticle.DescriptionYou want it to display only the first 80 characters:{{ article.Description|truncatechars:80 }}

If you want to truncate by word instead of character,truncatewordsIt is more appropriate, it will try to truncate at the complete word boundary, avoiding cutting a word in half, which is especially useful in English content.

For example, truncate the article title to the first 15 words:{{ article.Title|truncatewords:15 }}

Truncate HTML content processing:truncatechars_htmlwithtruncatewords_html

When you are dealing with content that contains HTML tags (such as the article summary output by a rich text editor), use it directly.truncatecharsortruncatewordsIt may destroy the HTML structure, causing the page to display abnormally. At this time, AnQiCMS provides specialtruncatechars_htmlandtruncatewords_htmlFilters. They can intelligently extract HTML content while ensuring the correct closure of tags, avoiding page chaos. When using these suffix filters, it is usually necessary to cooperate with_htmlsuffix filters, it is usually necessary to cooperate with|safeA filter that tells the template engine that this output is safe HTML, and it does not need to be escaped again to prevent the browser from displaying it as plain text.

For example, extract the first 100 characters of an article containing HTML while preserving the HTML structure:{{ article.FullContent|truncatechars_html:100|safe }}

Similarly, truncate HTML content by word:{{ article.FullContent|truncatewords_html:20|safe }}

More text beautification: case and line breaks

AnQiCMS also provides some convenient text processing filters besides truncation:

  • upper: Convert text to uppercase.
  • lowerLowercase the entire text.
  • titleCapitalize the first letter of each word.
  • capfirstCapitalize only the first letter of the text.

These filters can help you unify the display style of text according to your design needs.

For processing multi-line text,linebreaksbrthe filter can convert the newline characters in the text into\nHTML directly.<br/>Label, allowing multiline content to be displayed correctly on the web page.

{{ comment.Content|linebreaksbr|safe }}

Precise numbers, clearly conveying information

It is crucial to maintain a consistent and clear format when displaying prices, ratings, or any numbers with decimals. AnQiCMS's number filter is exactly for this purpose.

Format floating-point numbers: floatformat

floatformatThe filter can help us accurately control the display format of floating-point numbers, avoiding redundant decimal places or unnecessary zeros.

By default,floatformatIt will intelligently handle numbers. For example,{{ product.Price|floatformat }}It will display numbers like34.00000as such.34while34.23234displayed as34.2It will automatically handle the trailing zeros and retain one decimal place (if non-zero).

If you need more precise control, you can specify the number of decimal places, for example.{{ product.DiscountPrice|floatformat:2 }}It will always keep two decimal places, even if34.00000it will also be displayed as34.00. This is very practical for scenarios such as product prices that need to be displayed with two decimal places.

You can even specify a negative number as the decimal places, for example{{ product.Weight|floatformat:"-3" }}It will round to the third decimal place.

A more general formatting:stringformat

exceptfloatformat,stringformatThe filter provides more flexible number and text formatting capabilities, similar to Go language'sfmt.Sprintf()function. You can use various formatting verbs (such as%.2fto represent floating-point numbers with two decimal places,%dto represent integers,%sto represent strings) to accurately control the output format.

For example, you want to display the product rating as "Rating: 4.5 points":{{ product.Rating|stringformat:"评分:%.1f分" }}

Or embed an integer in a piece of text:{{ item.Views|stringformat:"已有%d人阅读" }}

Auxiliary number operations:addWith type conversion

For simple addition of numbers,addFilters can also be completed directly in the template, for example, if you want to display a hint that adds one to the quantity of items in the shopping cart:{{ item.Quantity|add:1 }}

In some cases, you may need to ensure that the variable is of the correct numeric type in order to format or calculate.integerandfloatThe filter can convert a string to an integer or floating-point number, which is very useful when handling numbers from non-strict data sources.

Common scenarios and practical suggestions

By flexibly using these filters, you can convert raw data into information that is easy to read and understand, greatly enhancing the professionalism and user experience of the website. For example:

  • The article title and summary in the news list can be usedtruncatewords_htmlto maintain the format.
  • The price on the product detail page can be unified usingfloatformat:2.
  • The comment content can be passed throughlinebreaksbrto maintain the layout and proceed throughtruncatecharsTruncate in preview.
  • Statistical data can be used according to requirements.stringformatAdd units or prefixes.

Some practical suggestions:

  1. **Always perform

Related articles

How to view the content that has been crawled and accessed in the website traffic statistics and crawler monitoring data?

Manage a website, what we care most about is whether our content is seen by users and whether it is indexed by search engines.This data is like the pulse of the website, directly reflecting the health of content operation and the activity of the website on the Internet.AnQi CMS knows these core needs and has built practical traffic statistics and spider monitoring functions, allowing us to easily master the key performance indicators of the website.Why are we so concerned about traffic and crawler data?In short, paying attention to these data is to better understand and optimize our website. First

2025-11-09

How are the "recommended properties" (such as headlines, slides) used to control the special display positions on the homepage or list?

In content operation, we often need to highlight specific content on the website, such as the latest news, popular articles, or important product recommendations.AnQi CMS provides a very practical feature - "Document Recommendation Attribute", which can help us easily achieve these diverse content display needs, making the website more attractive. ### Flexible and diverse document recommendation attributes When we enter the Anqi CMS backend to edit or publish documents, we will notice an option called "recommendation attributes".This provides a variety of preset identifiers, as if tagging the content with different labels

2025-11-09

How to introduce third-party JavaScript libraries (such as Markdown, mathematical formulas, flowcharts) in templates to enhance content display?

In AnQiCMS, introducing third-party JavaScript libraries is a very effective means to make your content display more expressive and professional.For example, writing clear text with Markdown, displaying complex mathematical formulas with MathJax, or using Mermaid to draw intuitive flowcharts can greatly enrich the user's reading experience.The powerful template system of AnQiCMS provides us with a flexible integration method.## AnQiCMS template system overview First

2025-11-09

Does AnQiCMS support displaying the document list under a specific Tag on the front end?

In AnQi CMS, you can of course easily display the document list under a specific Tag (label) on the frontend.This is not only a basic function of a content management system, but AnQi CMS also enables you to achieve this in a very fine and user-friendly manner through its flexible template tag system, thus better organizing content and improving the user experience and SEO performance of the website.Starting from version 2.1.0 of AnQi CMS, the system introduced powerful article and product Tag label features, which means you can now add one or more tags to your content (whether articles or products)

2025-11-09

How to correctly render Markdown document content as HTML in a template?

Content creators often choose Markdown as their preferred tool because it can organize content in a concise and clear manner, while also easily achieving formatting effects.However, simply inputting content in Markdown format in the background is not enough to allow the website front end to display it correctly. We need some additional steps to ensure that the content is rendered correctly as HTML.This article will introduce in detail how to make Markdown content turn beautiful in AnQiCMS templates, presenting it to users in the form of HTML.### One

2025-11-09

How to dynamically display copyright information and the current year at the bottom of a website or other areas?

In website operations, keeping information up to date is an important part of maintaining a professional image, and the copyright year at the bottom of the website is often overlooked. Manually modifying it every year is not only cumbersome but also prone to omission.Luckyly, using Anqi CMS, we can easily implement the dynamic display of copyright information, so that the information at the bottom of the website is always up-to-date.Why is dynamic copyright information so important?Imagine a user visiting a website and seeing the copyright year prominently displayed at the bottom.This could make the website look outdated and may give people an impression of unprofessionalism.For content operators

2025-11-09

How to automatically convert multiline text content to HTML format with <p> or <br> tags for display?

In website content display, we often encounter such needs: multi-line text edited in the background can be automatically converted into a format with HTML paragraph tags `<p>` or line break tags `<br>` on the front-end page, rather than simply being compressed into a long string of text.AnQiCMS provides a flexible and powerful method to solve this problem, whether it is through built-in editor features or by using template filters for precise control, it can be easily realized.### One, use the content editor to achieve automatic conversion When you go through AnQiCMS

2025-11-09

How to ensure that the URL strings in the content are automatically converted into clickable hyperlinks?

In daily website content operations, we often need to include various links in articles or descriptions, whether they point to other pages within the site or external references.One of the key aspects of user experience is that these URL strings can be automatically turned into clickable hyperlinks, rather than a series of cold texts.AnQiCMS (AnQiCMS) fully understands this and provides a variety of flexible and efficient methods to help us ensure that URLs in the content can be intelligently converted into clickable hyperlinks.Next, we will discuss how to achieve this goal in AnQiCMS

2025-11-09