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:
- Data cleaning: Remove specific prefix and suffix tags from the collected data, such as
"[原创]文章标题"or"产品名称 - 新品"of[原创]and- 新品. - 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. - User Input ProcessingWhen a user accidentally enters extra punctuation (such as
"联系电话:1234567890"of"联系电话:") before displaying it, it can be removed. - 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.