In AnQiCMS template development, dealing with and optimizing the display of page content is one of the daily tasks.We often need to process data obtained from the backend to ensure it is neat and meets expectations on the frontend.Amongst, cleaning up strings, especially removing extra whitespace characters or specific characters, is a key element in improving content quality and user experience.The AnQiCMS template system provides several very practical filters:trim/trimLeftandtrimRightThey each have their unique uses and application scenarios.
trimFilter: Fully remove leading and trailing characters.
trimThe filter is the most comprehensive of the three. Its main function is toRemove all default whitespace characters from both ends of a string (i.e., from the beginning and the end) or remove any specified characters.
Imagine that we import an article title from some data source or the user accidentally enters a few extra spaces in the form.If these extra spaces are directly displayed on the page, it may affect the layout aesthetics, and even in some cases, it may destroy the page layout.At this point, simply apply the title variabletrimA filter that can intelligently clean up all whitespace characters (including spaces, tabs, newlines, etc.) at both ends of a string, making the content look neater.
For example, if there is a variablepageTitlehas a value of" 欢迎使用安企CMS(AnQiCMS) "(String at both ends with spaces), use{{ pageTitle|trim }}After that, the output will become"欢迎使用安企CMS(AnQiCMS)".
What's more powerful is,trimThe filter is not limited to handling whitespace characters. If you want to remove specific characters from the beginning and end of a string, for example, some data may start and end with specific prefixes or suffixes, you can also specify a character set.For example, if your string is"欢迎使用安企CMS"And if you want to remove the characters "Welcome" and "CMS" at both ends, you can use it like this:{{"欢迎使用安企CMS"|trim:"欢迎CMS"}}. Here,"欢迎CMS"It is considered a character set, the filter will continuously remove any characters from both ends of the string that appear in this character set until it encounters a character that does not belong to the character set. Therefore, the output will be"使用安企"Because the characters "欢", "迎", "C", "M", "S" were recognized and removed.
trimLeftFilter: Precisely remove the characters on the left
withtrimdifferent,trimLeftThe filter will onlyRemove all default whitespace characters from the beginning (i.e., the leading part) of a string, or any specified characterIt does not perform any processing on the right side of the string.
This filter is very useful when you need to retain the format or content of the right side of the string, only cleaning the left side in cases. For example, you may have a block of text that contains indentation, but you don't want to remove the newlines or spaces that may exist inside or to the right of the text block, at this timetrimLeftit can be put to use.
For example, if the variableproductCodehas a value of" PRD-12345 "(with spaces on the left), use{{ productCode|trimLeft }}then the output will be"PRD-12345 "spaces on the left are removed while those on the right are retained."),
Similarly, if a character set is specified,trimLeftIt will start from the leftmost character of the string and remove any character that appears in the character set. For example,{{"欢迎使用安企CMS(AnQiCMS)"|trimLeft:"欢迎"}}It will remove the characters “欢” and “迎” from the beginning and output"使用安企CMS(AnQiCMS)".
trimRightFilter:定点剔除右侧字符
trimRightThe filter matches exactly withtrimLeftOn the contrary, it will onlyRemove all default whitespace characters from the right end of the string (i.e., the ending part), or any character you specifyIt does not process the left content of the string.
When we need to clean up the extra content at the end of the string,trimRightIt is an ideal choice. For example, the article summary may have some punctuation marks or line breaks at the end, or the file name at the end needs to remove the specific file type suffix.
For instance, if the variablecommenthas a value of"这是一条评论。 "(Space on the right), use{{ comment|trimRight }}then the output will be"这是一条评论。"The space on the right is removed, the content on the left remains unchanged.
When specifying a character set,trimRightIt will remove any character that appears in the character set from the rightmost side of the string. For example,{{"欢迎使用安企CMS(AnQiCMS) "|trimRight:") "}}It will remove the trailing ")" and spaces from the string, and output"欢迎使用安企CMS(AnQiCMS".
Summary of scenarios and differences
In general,trim/trimLeftandtrimRightThese three filters provide powerful capabilities for cleaning characters at the beginning and end of strings. Their main differences lie inthe scope of operations:
trim: operating on theBoth ends(beginning and end).trimLeft: Only operates on stringsOn the left(Start).trimRight: Only operates on stringsRight(End).
In addition, another important difference is whetherspecified the character set to be deleted:
- IfDefaultAny character set parameter, it will delete all by defaultWhitespace(spaces, tabs, newline characters, etc.).
- Ifspecifieda string as a parameter (for example
"欢迎CMS"), they will treat the parameter as athe character setRemove all *any* characters that appear in the character set from either end, rather than as a complete substring to match and delete.
Choose which filter depends on your specific needs. If you need to clean up irrelevant characters at both ends,trimThis is the most direct choice. If you are only concerned with the specific cleaning on the left or right side, thentrimLeftortrimRightIt can provide more precise control to avoid accidental modification of the content on the other end of the string.
Frequently Asked Questions (FAQ)
1. If I only want to remove the spaces in the middle of the string, thesetrimDoes the filter work?The answer is no.trim/trimLeftandtrimRightFilters only process the beginning and end of strings. They do not affect any characters in the middle of the string. If you need to remove spaces from the middle of the string, you may need to combinesplitThe filter splits the string into an array of words, processes it, and then usesjoinThe filter can be recombined, or usedreplaceThe filter performs a global replacement.
2.trimDoes the series filter match and delete in order when specified characters?It does not match in order. As described in the article, when you specifytrim/trimLeftortrimRighta string parameter such as"欢迎CMS"When, this parameter is considered a "character set". The filter checks the beginning (for ""},""), or the end (for ""},""), of the string and removes any character within this character set.trimandtrimLeftThe filter checks the beginning (for ""},""), or the end (for ""},""), of the string and removes any character within this character set.trimandtrimRightThe filter checks the beginning (for ""},""), or the end (for ""},""), of the string and removes any character within this character set.The filter checks the beginning (for ""},""), or the end (for ""},""), of the string and removes any character within this character set.Characters, in any order. It will continue to delete until it encounters a character not in the specified character set.
Will these filters modify the original string variable?I will not. In AnQiCMS (and most modern template engines), filters are 'non-destructive' operations.This means they do not directly modify the original variables you apply the filter to.On the contrary, they will return a processed oneNew stringAs output. The original variable value will remain unchanged, and you can continue to use it in other parts of the template without being affected.