How to use AnQiCMS filters to format and truncate output text content?

Calendar 👁️ 62

In the AnQiCMS content management system, the presentation effect of content often determines the user experience and the efficiency of information communication.To meet the diverse content display needs, AnQiCMS provides a powerful and flexible template filter function.These filters can help us with fine-grained processing of the original text during content display, whether it's adjusting text style, optimizing display length, or performing data conversion, it can be easily achieved.

Understand AnQiCMS template filters

From a technical perspective, the filter syntax of AnQiCMS is very intuitive, its core structure is like{{ 变量 | 过滤器名称: 参数 }}. By following a pipe symbol after a variable|Then specify the filter name and its possible parameters, and we can process the output content of the variable in real time.This mechanism makes content formatting and truncation extremely convenient, without modifying the original content, just controlling it at the template level.

flexible text formatting techniques

In website content operation, text formatting is a key factor in improving the readability and professionalism of the content. AnQiCMS provides a variety of filters to meet different formatting needs:

1. Title case format should be uniform in capitalizationImagine that we need to capitalize the first letter of the article title or paragraph, or convert it to uppercase or lowercase uniformly, or even capitalize the first letter of each word to meet the title specification. At this point,capfirst(First letter capitalized,)lower(All lowercase,)upper(All uppercase) andtitleThese filters can be put to good use, ensuring that the text maintains a uniform style across different display scenarios.

2. Precisely handle HTML contentWhen the content contains HTML tags, we may want these tags to be parsed by the browser normally, rather than displayed as plain text. At this time,safeThe filter is crucial, as it informs the template engine that the content is safe and does not need to be escaped, and can be output directly as HTML. For example, the main content of an article will usually use{{ archive.Content|safe }}Make sure the format is rendered correctly. In addition,renderThe filter is very useful when converting Markdown content to HTML. For the scenario where you need to retain line breaks in plain text and convert<br/>or<p>tags,linebreaksandlinebreaksbrProvided a convenient conversion method. If the goal is to remove all HTML tags,striptagsyou can clear it with one click; whileremovetagsCan accurately delete specified HTML tags, such as removing all<i>.

3. Standardization of numerical and time informationWhen displaying numerical information such as prices or statistics,floatformatThe filter helps us accurately control the decimal places of floating-point numbers, ensuring clear presentation of the numbers. If a more generic formatting output is needed,stringformatit provides a format similar to Go languagefmt.Sprintf()The powerful function, which can output various types of data in a customized pattern. Handling time information is another common requirement, AnQiCMS providesdateFilter (or its aliastimewilltime.TimeTime formatting of the type, as well as more commonly used for timestampsstampToDateFilter, which can easily convert a 10-digit timestamp into a readable date and time format, such as{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}.

4. Automated URL and Email LinkThe website content often includes URLs or email addresses, and we hope they can be automatically converted into clickable links for the convenience of users.urlizeThe filter can recognize these strings and wrap them in<a>tags, even automatically addingrel="nofollow"attributes to optimize SEO. If the link is too long,urlizetruncCan also convert to a link while intelligently truncating the displayed text and adding an ellipsis to keep the page tidy.

5. Flexible management of white spaces and specific characters in text.Sometimes there may be extra spaces, line breaks at the beginning and end of the text, or specific characters in the content need to be deleted.trimSeries filters(trim/trimLeft/trimRightIt can flexibly handle leading and trailing whitespace or specified characters.cutThe filter can remove specific characters from any position in a string, for example, removing all spaces or commas from the text.

6. Combination, splitting, and replacing of text.Concatenating multiple text segments or data is a common operation.addFilters can perform addition operations or concatenation on numbers or strings. When dealing with arrays,joinThe filter can join array elements into a string with a specified delimiter; conversely,splitThe filter can split a string into an array with a delimiter.make_listCan also split a string into an array of characters. If you need to replace specific keywords in the text,replaceThe filter provides an accurate find and replace feature, for example, replacing all occurrences of 'AnQiCMS' with 'AnQiCMS'.

Effective content truncation strategy

On list pages or in the summary display area, appropriately truncating text content is an important means to improve page loading speed and visual aesthetics. AnQiCMS provides various truncation filters:

1. Truncate by character count (truncatechars,truncatechars_html)Under scenarios such as article summaries, list previews, etc., we often need to truncate text to control display length.truncatecharsThe filter truncates by character count and adds an ellipsis (…) at the end. It is worth noting that the ellipsis itself is counted in the character count. If the content contains HTML tags,truncatechars_htmlIt can intelligently truncate while maintaining the integrity of the HTML structure, avoiding page display errors, which is a very practical feature when using rich text in summaries.

2. Truncate by word count (truncatewords,truncatewords_html)If you want to truncate more "semantically", avoid truncating words,truncatewordsThe filter truncates based on the number of words, which is particularly important in English content to maintain the integrity of the sentence. Similarly, its HTML versiontruncatewords_htmlCan maintain word integrity and structure when processing HTML content, avoiding the truncation of half of the HTML tags or words.

3. Flexible slicing operation (slice)For more flexible slicing needs, such as starting from a certain position in a string or array to another position,sliceThe filter provides powerful slicing capabilities, allowing precise control over the start and end positions, which is very useful for scenarios that require dynamically extracting specific parts of the content.

It

Related articles

How to display the friend link list in AnQiCMS?

In AnQiCMS, managing and displaying friendship links is very important for improving the website's search engine optimization (SEO) performance, increasing the number of external links, and promoting traffic interflow between websites.AnQiCMS as a fully functional content management system provides an intuitive backend operation and flexible template tags, allowing you to easily display a list of friend links on your website. ### One, manage friend links in AnQiCMS background Firstly, you need to add and manage friend links in the AnQiCMS background.

2025-11-08

How to display the TDK (title, description, keywords) information of the website in AnQiCMS template?

AnQiCMS is a content management system that pays great attention to SEO optimization, providing strong support for the search engine performance of website content.Among them, TDK (Title, Description, Keywords, that is, title, description, keywords) is a core element of SEO optimization, and it is crucial to display this information correctly in the website template to enhance the visibility of the website in search engines.This article will introduce how to effectively display these TDK information in the AnQiCMS template.###

2025-11-08

How to set and display the navigation menu of the AnQiCMS website, does it support multi-level structure?

The website navigation is an important guide for users when visiting the website, a clear and easy-to-use navigation menu can greatly enhance the user experience.AnQiCMS offers very flexible and powerful functions in terms of website navigation settings and display, allowing us to easily manage and implement multi-level menu structures to meet the needs of various website layouts.In the AnQiCMS backend, all settings related to website navigation are concentrated in the "Navigation Settings" module.Enter this page, you will first see a "navigation category management" area.This feature is very useful

2025-11-08

How to make AnQiCMS automatically publish content at a specific time and display it?

In website content operation, maintaining a regular update and release rhythm is the key to attracting and retaining users.However, manually releasing content at specific time points is not only time-consuming but also prone to limitations imposed by human factors.AnQiCMS provides a very practical feature that can help you easily achieve scheduled content release and automatic display, making your website operation more efficient and automated.

2025-11-08

How does AnQiCMS ensure the display quality of image resources and perform automatic compression and WebP conversion?

In the era where content is king, the image resources on the website play a crucial role.They not only directly affect the beauty of the page, but are also key factors in determining the loading speed and user experience of the website.For operators, how to ensure the display quality of images while also taking into account the performance and storage costs of the website is undoubtedly a question worthy of reflection.AnQiCMS (AnQiCMS) is well-versed in this, providing a comprehensive image resource management and optimization strategy, aimed at helping users easily master this challenge.The foundation for ensuring the quality of image display

2025-11-08

How to integrate and display Markdown, mathematical formulas, and flowcharts correctly on the AnQiCMS page?

In AnQiCMS, adding Markdown, mathematical formulas, and flowcharts to content greatly enhances its expressiveness and readability, especially for technical documents, educational materials, or data analysis reports.AnQiCMS as an efficient content management system, provides flexible mechanisms to support these modern content display needs. To implement the correct display of Markdown, mathematical formulas, and flowcharts on the AnQiCMS page, it mainly involves enabling backend settings and the necessary introduction of frontend templates.Here are the detailed steps and some practical suggestions

2025-11-08

How to display dynamic breadcrumb navigation in AnQiCMS and customize the home page name?

In website content management, breadcrumb navigation plays a crucial role.It not only helps visitors clearly understand their position on the website, provides a convenient path to return to the upper page, but also reflects the hierarchical structure of the website from the side, which is greatly beneficial to user experience and search engine optimization.AnQiCMS as a powerful content management system naturally provides a flexible way to manage and display this kind of navigation.

2025-11-08

How to use the AnQiCMS document list tag to display content for specific categories or recommended attributes?

In AnQi CMS, efficiently managing and displaying website content is the key to improving user experience and website operation effectiveness.Whether you want to display the latest articles of a specific category on the homepage or highlight products with the "recommended" attribute, the document list tag (`archiveList`) provided by AnQiCMS is the core tool to meet these needs.Its flexible parameter configuration allows us to accurately control content retrieval, transforming technical details into easily accessible operational strategies.

2025-11-08