AnQiCMS `trim` family filter can delete which custom characters besides spaces?

Calendar 👁️ 59

In AnQi CMS template design, we often need to process the displayed data to ensure that the content presented to the user is both beautiful and accurate.Among them, string processing is an indispensable part of content operation.trimThe family filter is one of the very practical groups.

At first, we might thinktrimThe filter is mainly used to remove whitespace characters from both ends of a string, such as extra spaces or newline characters. This is indeed its basic function, but for AnQi CMS'strimThe family filter goes beyond this. In addition to the default whitespace characters, they also allow us to customize any character set to be deleted.

ThistrimThe family mainly includes three members:trim/trimLeftandtrimRight. They each carry out different cleaning tasks:

  • trimFilter: Responsible for cleaning the specified characters from both ends (beginning and end) of the string.
  • trimLeftFilter: Specifically for cleaning the specified characters from the beginning part of the string.
  • trimRightFilter: Focus on cleaning the specified characters from the end of the string.

When we are using these filters, if we just write{{ obj|trim }}It will intelligently remove all whitespace characters at both ends of the string, including spaces, tabs, and newline characters, etc.This is very convenient for cleaning user input or irregularly blank content obtained from external data sources.

However, the truly powerful aspect lies in the fact that we can specify a 'character set' for these filters, allowing them to remove any custom characters except whitespace.The character set is very important, which means you provide the filter with a set of independent characters, rather than a substring that needs to be fully matched.The filter checks the beginning or end of the string and removes any character from the character set until it encounters a character not in the set.

For example, suppose we have a string"欢迎使用安企CMS(AnQiCMS)"If you want to remove the "Welcome" at the beginning and the "CMS" at the end, we might intuitively think of writing it like this:{{"欢迎使用安企CMS(AnQiCMS)"|trim:"欢迎CMS"}}.At first glance, we expect it to delete the words 'Welcome' and 'CMS'.But since it processes a character set, it actually removes all the characters '欢' and '迎' at the beginning of the string, as well as all the characters 'C', 'M', and 'S' at the end."使用安Qi". Because it removed '欢' and '迎' from the left, and 'S', 'M', 'C' from the right, the remaining is '使用安Qi'.

For example, if we need to get a filename from"report.pdf!!!"Remove the exclamation mark at the end of Chinese, and it can be usedtrimRightThe filter specifying the character set to be removed"!"like this:{{"report.pdf!!!"|trimRight:"!"}}. The result will be"report.pdf".

Similarly,trimLeftAlso follow this rule. If you have a string"### My Title", want to remove the leading"#"character, you can use{{"### My Title"|trimLeft:"#"}}to get the result will be" My Title".

This flexible character set deletion capability is very useful in various content operation scenarios. For example:

  1. Data cleaning: Remove specific prefix and suffix tags from the collected data, such as"[原创]文章标题"or"产品名称 - 新品"of[原创]and- 新品.
  2. URL beautificationIn some cases, it may be necessary to clean up unnecessary symbols in the URL path, although pseudo-static rules are more commonly used, but for specific outputs,trimit can also assist.
  3. User Input ProcessingWhen a user accidentally enters extra punctuation (such as"联系电话:1234567890"of"联系电话:") before displaying it, it can be removed.
  4. Uniform content formatEnsure that there are no specific delimiters or decorators at the beginning and end of list items or titles.

In summary, of Anqi CMS'strimThe family filter provides us with powerful string processing capabilities, which can not only handle common whitespace characters, but also accurately remove any custom characters at the beginning and end of the string according to the character set we provide.Mastering this feature will greatly enhance our efficiency and flexibility in handling and displaying content in templates.


Frequently Asked Questions (FAQ)

1.trimWhat characters does the filter delete by default when no custom characters are specified?

When you do nottrimThe family filter provides any custom character when, for example, just using{{ obj|trim }}They will default to removing all common whitespace characters from both ends of the string, including spaces ( ) and tabulators ( \t), and newline characters (\n) and newline character (\r) etc. This is very useful in cleaning user input or external data to ensure the content is tidy.

2. What filter should I use to remove a specific 'word' or 'phrase' from the beginning or end of a string, rather than a single character?

trimThe family filter works based on a 'character set', which deletes any single character found at a specified position, rather than a complete word or phrase. If you need to delete a specific substring (such as 'old phrase'), thenreplaceThe filter would be a more suitable choice. You can use it like this:{{ obj|replace:"旧短语,新短语" }}If you only need to delete once and the phrase is at the beginning or end, you may need to coordinate with other logical judgments.

3.trim/trimLeftandtrimRightAre you case-sensitive when processing custom character sets?

Yes, the Anqi CMS'strimThe family filter is case-sensitive when processing custom character sets. For example, if you specify the character set to be deleted is"abc"It will only delete lowercase letters 'a', 'b', 'c', and will not affect uppercase letters 'A', 'B', 'C'.Therefore, when defining the character set to be deleted, make sure to include all uppercase and lowercase forms that need to be considered.

Related articles

How to safely convert a user input numeric string to an integer or floating-point number for calculation in AnQiCMS template?

In daily website operations, we often encounter the need to display or calculate user input data on the webpage.Even when fields are explicitly set as numeric types in the background, when the data is fetched to the front-end template for rendering, they are often in the form of strings.This is not a problem when displaying simple content, but once it involves numerical calculations, such as calculating the total price, calculating percentages, etc., direct string operations can lead to unexpected results and even errors.

2025-11-07

What advanced formatting options does the AnQiCMS `stringformat` filter support (such as outputting percentages, scientific notation)?

In AnQiCMS template development, the flexibility of data display is often the key to determining user experience and the professionalism of content presentation.Among them, the `stringformat` filter is undoubtedly a powerful tool that allows us to finely format various data types to meet advanced needs from simple numerical precision control to complex percentages, scientific notation, and other advanced requirements.

2025-11-07

How to get the first or last image address of an array (such as an image list) in AnQiCMS template?

In Anqi CMS template design, flexibly displaying and managing images is an indispensable part of building a high-quality website.When the content contains multiple images, such as group images on product detail pages or article illustrations, we often need to accurately extract one of the images, such as using the first image as a thumbnail or cover, or obtaining the last image for special display.This article will discuss in detail how to easily obtain the address of the first or last image in the image array of the AnQiCMS template.

2025-11-07

How does the AnQiCMS `count` filter calculate the total number of times a specific keyword appears in the article content?

## Deep Analysis: AnQiCMS `count` filter counts the number of keyword occurrences in article content In daily website operations, we often need to understand certain key information in the article content, such as how many times a specific keyword appears in the article.This is crucial for SEO optimization, content quality assessment, or internal audit.AnQiCMS as an efficient content management system provides rich template tags and filters, making this work simple and intuitive. Today

2025-11-07

How to URL encode query parameters in AnQiCMS template to avoid conflicts with special characters?

When building dynamic links in the AnQiCMS template, we often need to pass variables as query parameters in the URL.For example, a search results page may need to pass the user's search term as a parameter;A category filter page may need to include the selected category ID or multiple filter conditions.However, these dynamic contents often contain special characters, such as spaces, `&`, and `?`Backticks, equals, slash, hash, etc., they have specific meanings in URLs.If these special characters are not processed, the browser or server may not be able to correctly parse the URL, resulting in a page error

2025-11-07

How to count word numbers with the `wordcount` filter of AnQiCMS when processing mixed Chinese-English text?

In Anqi CMS template design, the `wordcount` filter is a practical tool used to count the number of words in the text.For operation personnel and content creators, understanding the working principle, especially the statistical logic when dealing with mixed Chinese and English text, can help us accurately assess content length, optimize article structure, and better meet the needs of search engine optimization (SEO) and user reading experience.### `wordcount` filter basic usage The `wordcount` filter is very straightforward to use

2025-11-07

How does the AnQiCMS `wordwrap` filter implement intelligent automatic line breaks for long English paragraphs?

In daily content operations, we often encounter such a scenario: when long English paragraphs are published, they may exceed the container width on different devices or screen sizes, causing horizontal scroll bars to appear, which greatly affects the user's reading experience and the beauty of the page.AnQiCMS (AnQiCMS) fully understands this pain point and has provided a very practical template filter - `wordwrap`, which can cleverly solve the problem of automatic line breaking for long text.

2025-11-07

How to use the `yesno` filter of AnQiCMS to display 'yes', 'no', or 'unknown' status based on boolean values in the template?

In AnQi CMS template development, we often need to display different text in a user-friendly manner on the front page according to the boolean (true/false) status of background data, such as "yes" or "no."}Directly displaying `true` or `false` may seem too stiff, and writing complex `if-else` judgment statements can make template code look lengthy.Fortunately, AnQiCMS provides a simple and efficient solution - the `yesno` filter, which can help us easily convert boolean values to custom text states

2025-11-07