How to use AnQiCMS filters to truncate or convert text to uppercase and lowercase?

Calendar 👁️ 73

AnQiCMS provides strong flexibility in content display, its template engine is built-in with a rich set of filters, helping users to easily process text without modifying the original content, including truncation and case conversion.These filters make the presentation of front-end content more accurate and beautiful, meeting the display needs of different scenarios.

In AnQiCMS template syntax, the use of filters is very intuitive. You can add a pipe symbol after the variable name to|)Apply the filter, if the filter requires parameters, then use a colon after the filter name(:)Pass. The basic format is usually{{ 变量 | 过滤器名称 : 参数 }}The ability to perform chained operations makes text processing efficient and concise.

Flexible text truncation techniques

In website operations, we often need to condense long content into concise summaries or titles to adapt to card layouts, list displays, or maintain page neatness.AnQiCMS provides various text truncation filters to meet the needs of different scenarios.

For example, to truncate plain text content, you can usetruncatecharsandtruncatewords.truncatecharsThe filter will truncate text based on the number of characters, for example: {{ 文章标题 | truncatechars:15 }}The article title will be truncated to a maximum of 15 characters, and the part exceeding this will be displayed as “…”. It should be noted that “…” itself also occupies character space.

If you need to truncate based on the number of words,truncatewordsa filter would be a better choice:{{ 文章描述 | truncatewords:10 }}It extracts the first 10 words of the article description and ends with “…”. This is particularly common in English content to avoid reading disconnection caused by truncating words.

When the text content contains HTML tags, it is used directlytruncatecharsortruncatewordsIt may destroy the HTML structure, causing the page to display abnormally. At this time,truncatechars_htmlandtruncatewords_htmlThe filter comes into play. It can intelligently truncate HTML content while maintaining the integrity of tags, ensuring the correct rendering of the page. For example,{{ 文章内容 | truncatechars_html:50 | safe }}You can extract 50 characters (including HTML tags) while ensuring that the truncated HTML structure remains valid. Here is thesafeThe filter is necessary to inform the template engine that the output content is safe HTML and does not require escaping.

In addition to the above common truncation methods,sliceThe filter provides a more general string slicing function. It can cut strings or arrays according to specified start and end indices, for example{{ 文本 | slice:"1:5" }}It will extract the second to fifth characters (index starting from 0). For only getting the beginning or end of the text,firstandlastThe filter can quickly obtain the first or last character of a string, for example{{ 文本 | first }}.

In some cases, it may be necessary to remove characters from the beginning or end of a string, or from a specific position.trimThe filter can remove spaces or specified characters from the beginning and end of a string,trimLeftandtrimRightwhich is used to delete characters from the left or right. AndcutThe filter is more powerful, it can remove specified characters at any position in the string.These filters are very useful for cleaning and formatting text, such as removing extra punctuation or prefixes.

text case conversion

The uniform formatting of text case is crucial for layout and content presentation. AnQiCMS provides a series of filters that can easily convert text case.

upperThe filter can convert all letters in a string to uppercase:{{ 变量 | upper }}It will convert 'hello world' to 'HELLO WORLD'.

lowerThe filter is the opposite, converting all letters to lowercase:{{ 变量 | lower }}It will convert “HELLO WORLD” to “hello world”.

For situations where you need to capitalize the first letter of a sentence,capfirsta filter can achieve this function:{{ 变量 | capfirst }}it will convert 'hello world' to 'Hello world'.

If you want the first letter of each word to be capitalized, for example for titles, thentitleThe filter is the ideal choice:{{ 变量 | title }}It will convert 'hello world' to 'Hello World'.

These case conversion filters provide great convenience in handling user input, standardizing display formats, and unifying website styles.

Integrated application and **practice

The AnQiCMS filter can not only be used alone but also chained to combine multiple filters, achieving more complex text processing logic. For example, you may want to convert the title of an article to lowercase and then truncate it to the first 20 characters:{{ article.Title | lower | truncatechars:20 }}

When applying filters, especially when dealing with content that may contain HTML, be sure to pay attention to the choice of filters. As mentioned earlier, for HTML content, it is recommended to prioritize filters with_htmlSuffix filter to avoid breaking the page structure. Also, don't forget to add it when you confirm that the output content is safe HTML.| safeFilter to prevent content from being escaped twice.

By flexibly using these text truncation and case conversion filters of AnQiCMS, you can finely control the presentation of website content, improve user experience, and ensure the uniformity and professionalism of the website interface.

Frequently Asked Questions (FAQ)

  1. **truncatecharsandtruncatechars_html

Related articles

How to use a for loop to traverse data and display it in the AnQiCMS template?

AnQiCMS with its flexible and powerful template system makes content display efficient and expressive.For website operators and developers, mastering how to traverse and display data in templates is a key step to unlocking their powerful features and bringing the website content to life.Today, let's delve into how to use the `for` loop in AnQiCMS templates to easily present your dynamic data.AnQiCMS's template engine adopts syntax similar to Django, which allows users familiar with other mainstream template languages to quickly get started.

2025-11-08

How to implement conditional judgment in AnQiCMS template to control the display of content?

In AnQi CMS template design, flexibly controlling the display of content is the key to building dynamic, responsive websites.Whether it is to display different information based on page type, data status, or specific conditions, conditional judgment is an indispensable tool.The AnQiCMS template engine provides an intuitive and powerful conditional judgment mechanism, allowing you to easily implement these complex logic.### Core Grammar: The Basics of Conditionals The condition judgment in AnQiCMS templates is similar to many mainstream template engines, using the `{% if ... %}` tag structure

2025-11-08

How to format a timestamp and display it as a readable date and time in AnQiCMS?

In website content operation, time information plays an indispensable role.Whether it is the publication time of the article, the shelf date of the product, or the submission time of the comments, a clear and readable date and time format can greatly enhance the user experience.AnQi CMS as an efficient content management system, fully considers this point, and provides a flexible way to format and display these timestamp data.### Understanding Timestamps in AnQi CMS In the AnQi CMS backend, when we publish articles, products, or perform other content management operations

2025-11-08

How to get and display the list of friendship links configured in the AnQiCMS background?

In website operation, friendship links play an indispensable role. They not only bring valuable external traffic to the website but also help improve search engine optimization (SEO) effects, enhance the authority and credibility of the website.For users using AnQiCMS, managing and displaying friend links is a simple and efficient process.This article will introduce in detail how to configure friend links in the AnQiCMS background, as well as how to elegantly present them in your website front-end template.In AnQiCMS admin manage friend links First

2025-11-08

How to display the mobile URL of the website in AnQiCMS template?

When using AnQiCMS to build and manage websites, many friends may encounter such a need: If the website has an independent mobile version, how can you conveniently obtain and display the URL of this mobile website in the template?This makes it convenient for users to switch between different devices, and it also has a positive impact on search engine optimization (SEO).Don't worry, AnQiCMS provides a very intuitive way to achieve this.Why do we need to display the mobile URL in the template?First, let's talk about why there is such a need

2025-11-08

How to display the path of static files (CSS/JS/images) in AnQiCMS template?

When building a website with AnQiCMS, the correct reference to static files (such as CSS stylesheets, JavaScript scripts, and images) is the basis for ensuring the normal operation and beautiful presentation of the website.AnQiCMS provides us with a clear and flexible mechanism for managing and displaying these file paths, whether it is the static resources built into the template or the media files uploaded through the backend, there is a convenient way to reference them.

2025-11-08

How to implement multi-language content switching and display in AnQiCMS?

Today, having a website that supports multiple languages has become a key factor for enterprises to expand into international markets and enhance user experience.AnQiCMS is an efficient and flexible content management system that fully considers this need, providing you with a convenient multi-language content switching and display solution. Different from simply stacking multiple languages on the same page, AnQiCMS adopts a more flexible and powerful multi-site management mode to achieve multilingual support.

2025-11-08

How does AnQiCMS control the display and hiding of content based on user group permissions?

When building and operating a website, we often encounter such needs: some content is intended for everyone to see, while others are designed specifically for certain user groups, such as paid membership exclusive, VIP content, or materials for internal team review.AnQiCMS provides a powerful and flexible user group permission control mechanism that can help us easily achieve fine-grained display and hide of content.

2025-11-08