How to use AnQiCMS filter to perform string truncation, case conversion, and other processing on displayed content?

Calendar 👁️ 60

When using AnQiCMS for website content management, we often encounter situations where we need to make some minor adjustments to the displayed content, such as when a title of an article is too long and we want it to be displayed only partially with an ellipsis at the end;Or some text is desired to be displayed in uppercase or lowercase. The powerful template filter function of AnQiCMS can easily help us meet these needs without modifying any backend code, and it can be completed directly at the template level.

The template filter is a practical tool in AnQiCMS template language, which allows us to transform, format, or process the value of template variables before outputting them to the page. Its usage is very intuitive, usually by adding a vertical bar after the variable.|Follow the filter name, and if the filter requires parameters, use a colon after the filter name (:Pass). For example,{{ 变量名 | 过滤器名称 : 参数 }}.

Content truncation processing

In many scenarios, we may need to truncate long text content to maintain the page layout or provide a brief introduction.AnQiCMS provides several convenient filters for truncating strings and HTML content.

If we are dealing with plain text, we can usetruncatecharsortruncatewordsfilter.truncatecharsIt will truncate the string according to the number of characters and add an ellipsis at the end. For example, you have an article title{{ item.Title }},Hope it shows no more than 15 characters, you can write it like this: {{ item.Title | truncatechars:15 }}This will truncate the title to a maximum of 15 characters (including an ellipsis), and if the original title is fewer than 15 characters, it will not change.

AndtruncatewordsIt is truncated by word count. If you want the title to display a maximum of 5 words, you can do it like this:{{ item.Title | truncatewords:5 }}This filter ensures that truncation occurs at word boundaries and an ellipsis is added at the end.

If our content contains HTML tags and we want to truncate without destroying the HTML structure, AnQiCMS also provides the corresponding HTML safe truncation filter:truncatechars_htmlandtruncatewords_html. They are similar to plain text truncation filters, but they intelligently handle HTML tags to ensure that the truncated HTML code is still valid.For example, for an article summary that includes HTML:{{ item.Description | truncatechars_html:50 | safe }}HeresafeThe filter is very important, it tells the template engine that this output is safe HTML, which does not need to be escaped, so that the browser can correctly parse the truncated HTML.

Convert string case

The display format of unified content is crucial for improving user experience and website professionalism. AnQiCMS' filter can help us easily achieve case conversion.

The most direct case conversion isupperandlower.upperIt will convert all English letters in the string to uppercase:{{ item.Title | upper }}AndlowerIt will convert all letters to lowercase:{{ item.Title | lower }}

Sometimes we only need to capitalize the first letter of a string, and for this we can usecapfirst. It will only capitalize the first letter of the string, leaving the rest unchanged:{{ "hello world" | capfirst }}It will be displayed asHello world.

If you want the first letter of each word in a string to be uppercase and the rest lowercase, thentitleThe filter will be your good helper:{{ "anqicms is a great cms" | title }}It will be displayed asAnqicms Is A Great Cms. This is very useful for formatting article titles or names.

Other common string processing techniques

In addition to truncation and case conversion, AnQiCMS also provides a series of practical string processing filters to meet our diverse content operation needs.

  • Remove characters (cut): If you want to remove a specific character or substring from a string,cutthe filter can do that. For example, to removeAnQiCMSfromiyou can write{{ "AnQiCMS" | cut:"i" }}The result isAnQCMS.
  • Replace characters (replace): When you need to replace a part of a string with another part,replacethe filter comes into play. The syntax is{{ obj | replace:"旧内容,新内容" }}For example,{{ "Hello World" | replace:"World,AnQiCMS" }}It will output.Hello AnQiCMS.
  • Remove spaces (trim,trimLeft,trimRight)These filters are used to remove spaces or specified characters from both ends, the left side, or the right side of a string.{{ " AnQiCMS " | trim }}Will becomeAnQiCMS.
  • Concatenation and Splitting (join,split):splitYou can split a string into an array by delimiter, andjoinYou can concatenate an array into a string with a separator. This is very convenient when dealing with list data such as tags, keywords, etc. For example,{% set tags = "tag1,tag2,tag3" | split:"," %}You can convert a string into an array, then{% for tag in tags %}{{ tag }}{% endfor %}output in a loop.
  • Get the length (length): When you need to know how many elements a string, array, or object has,lengthThe filter can quickly give you the answer.{{ item.Title | length }}It will return the number of characters in the title.

These filters can be used individually, or chained, which means applying multiple filters to a variable in sequence, from left to right. For example:{{ item.Description | truncatechars:100 | lower | safe }}This will first truncate the description, then convert the truncated text to lowercase, and finally output it as safe HTML.

Mastering these filters allows us to have greater flexibility and fine control in AnQiCMS template design and content display, thereby better presenting website content and enhancing user experience.


Frequently Asked Questions (FAQ)

  1. Ask: Can the filter be used for all types of variables?Answer: Filters are mainly used to process data types such as strings, numbers, arrays (or Go language slices), and mappings (or Go language maps).But each filter has its specific application scenarios and expected input types. For example,upperThe filter is only valid for strings, andlengthFilters can be used for strings, arrays, or mappings. In actual use, if the input type does not meet the requirements of the filter, it may cause the filter to fail to function normally or to output null values.
  2. Ask: If I need to truncate content containing HTML without losing its style or structure, which filter should I use?Answer: In this case, you should usetruncatechars_htmlortruncatewords_htmlFilter. These two filters will intelligently handle HTML tags, ensuring that the truncated HTML structure remains valid and that styles are not lost.At the same time, please be sure to add after truncation|safeA filter that tells the template engine that this is a safe HTML fragment, to avoid the content being escaped again and causing HTML tags to be displayed directly.
  3. Ask: Can I apply multiple filters at the same time? What is the execution order?Answer: Yes, you can apply multiple filters to a variable, which is called 'chaining'.The execution order of the filters is from left to right. This means that the first filter will process the value of the original variable, then the second filter will process the output of the first filter, and so on. For example,{{ "HELLO WORLD" | lower | truncatechars:5 }}Will first convert the string to lowercase ("hello world"), then truncate it to 5 characters ("hello…").

Related articles

How does AnQiCMS ensure that mathematical formulas and flowcharts inserted in the Markdown editor are displayed normally on the front end?

In modern website content creation, Markdown is favored by a large number of content creators for its concise and efficient features.However, for websites that need to present complex information, such as mathematical formulas and flowcharts, how to seamlessly insert them into Markdown editors and ensure they are displayed normally on the front end is often a challenge for content operators.AnQiCMS (AnQiCMS) fully understands this need, through flexible configuration and standardized introduction methods, allowing these complex elements to be elegantly presented on your website

2025-11-08

How to display custom Banner image and content in AnQiCMS single page?

In website operation, a well-designed single page often plays an important role in brand display, product introduction, or specific event promotion.A striking Banner area that can quickly capture the visitor's attention and convey core information.AnQiCMS as a flexible content management system, provides a very convenient way, allowing you to easily set and display custom Banner images and content on a single page.Next, we will explore how to implement this feature in AnQiCMS.## Step 1

2025-11-08

How to batch update display text and links for the 'Full Site Content Replacement' feature of AnQiCMS?

The "Full Site Content Replacement" feature of AnQiCMS: an efficient tool for batch updating website text and links Content updates and maintenance are an indispensable part of daily website operations.It is undoubtedly time-consuming and labor-intensive to manually modify a massive amount of content, whether it is to adapt to a new content strategy, optimize search engine rankings, or simply correct a spelling error or update a broken link.Fortunately, AnQiCMS provides a powerful feature named "Site-wide Content Replacement" which can help users complete these tasks in bulk and efficiently, greatly improving operational efficiency

2025-11-08

How to build a multi-level dropdown menu and display the navigation list tags in AnQiCMS?

The website navigation serves as the core entry point for users to explore the website's content, and its structural design directly affects user experience and information acquisition efficiency.Especially multi-level dropdown menus can effectively organize a large amount of information, making the website structure clearer and more hierarchical.In Anqi CMS, building and displaying such a multi-level navigation system is flexible and intuitive. To successfully build a multi-level dropdown menu, we need to combine the backend navigation configuration with the tag call of the frontend template.### Step one: Understand the data structure of the Anqi CMS navigation list and the background configuration In Anqi CMS

2025-11-08

How to automatically parse URL strings in HTML code to display clickable links in AnQiCMS?

In the daily operation of websites, we often need to insert various URLs in the article content, whether it is to refer to external resources or to guide users to visit other pages within the site.If adding the `<a>` tag manually to these URLs each time is time-consuming and prone to errors.It is fortunate that AnQiCMS provides users with a very intelligent and convenient solution in this regard, which can automatically identify and parse URL strings in HTML code and convert them into clickable links.### Core Mechanism: Make the URLs in the content "alive" When you edit articles in the AnQiCMS backend

2025-11-08

How to generate random text in AnQiCMS template as placeholder content for development testing display?

During the development of AnQiCMS templates, we often need to fill in placeholder text on the page before the real content is ready, in order to better observe the layout, test the style and function.Manually entering "test content" or placeholder text is time-consuming and lacks authenticity.Fortunately, AnQiCMS provides a very convenient built-in tag that can elegantly solve this problem - that is the `lorem` tag.### AnQiCMS is a random text generator tool: `lorem` tag `lorem`

2025-11-08

How does AnQiCMS display the table of contents or outline of the article content on the article detail page?

For articles that are rich in content and long in length, a clear table of contents or outline is like a navigation map, which can greatly enhance the reading experience of the reader.It not only helps readers quickly grasp the main points of the article, but also allows them to accurately jump to the parts of interest, thereby improving the readability and practicality of the content.In AnQiCMS, implementing the content directory display on the article detail page is not a difficult task, the built-in functions of the system provide us with a simple and efficient solution.### AnQiCMS How to understand the article structure? AnQiCMS

2025-11-08

How to dynamically retrieve and display the contact information of a website, such as phone number, email, and social media links?

The contact information of the website, such as phone number, email, and social media links, is an important bridge for users to connect with us.It is crucial that the accuracy and accessibility of this information is maintained in the operation of the website.AnQiCMS as an efficient content management system provides a very convenient way to manage and dynamically display this contact information, allowing us to easily maintain the consistency of website content and quickly update it when needed.This article will delve into how to set contact information in AnQiCMS, as well as how to dynamically retrieve it in templates and display it in a user-friendly manner.### One

2025-11-08