In Anqi CMS template development, we often need to control and adjust the content displayed on the page in detail.The filter is a set of powerful tools provided by the Anqi CMS template engine that help us easily change the output format and content of variables.Today, we will focus ontrimLeftandtrimRightThese very practical filters play a key role in handling extra content at the beginning and end of strings. To better understand them, we will also briefly mention their 'brother' filterstrim.

UnderstandingtrimFilter: Clean both ends of the string

Further understandtrimLeftandtrimRightBefore that, let's take a look at first.trimfilter.trimThe function is to remove the suffix of the string.All leading and trailing spaces or specific characters.

If you just use it simply.{{ 你的变量|trim }}It automatically removes all extra spaces, tabs, and newline characters from the beginning and end of the string. This is very convenient for standardizing input data or removing unexpected blank spaces caused by layout.

When you want to remove specific characters, you can pass these characters as parameters totrimFor example,{{ "欢迎使用安企CMS"|trim:'欢迎CMS' }}. It should be noted that it checks if the parameters are present at both ends ofany characterRemove it and continue until a character not in the set is encountered. For example, in the above example, it removes the '欢' and '迎' at the beginning, as well as the 'C', 'M', and 'S' at the end, resulting in使用安企.

trimLeft:Precisely delete the left part of the string

As the name implies,trimLeftThe filter focuses on processing the string'sleft (i.e., the beginning)part. Its behavior is similar totrimbut its scope of action is limited to the beginning of the string.

If you have a string欢迎使用安企CMS(Note that there are spaces at the beginning and end), use{{ " 欢迎使用安企CMS "|trimLeft }}, and the result will be欢迎使用安企CMS. As you can see, the spaces on the right side of the string are still preserved."),

Similarly, you can specify the character set to be removed. For example,{{ "欢迎使用安企CMS"|trimLeft:"欢迎" }}Remove '欢' and '迎' characters from the beginning of the string (if they exist), get使用安企CMS. This filter is very useful when we need to remove a fixed prefix or clean up unnecessary leading characters from user input, for example, when processing CSV data, it may be necessary to clear the specific delimiter at the beginning of the line.

trimRight: Precisely delete the right part of the string

trimRightThe filter works the opposite, it focuses on removing the string'sright side (i.e., the end)part. It will remove all the extra spaces at the end of the string or the specific characters you specify.

For example,{{ " 欢迎使用安企CMS "|trimRight }}The result is欢迎使用安企CMS. The leading spaces in the string are preserved.

If you want to remove specific characters at the end of the string, like{{ "欢迎使用安企CMS(AnQiCMS) "|trimRight:") " }}It will remove all ')' and spaces at the end, resulting in `Welcome to AnQiCMS(AnQiCMS)